例:一个数组,int[] arr =new int[]{1022,1052,1084,1123,2007,2077,2129,3025,3062,3064,3066,3070,3101,3133 };
规则: 各个数字按第一位数出现次数由多到少,第一位数递增或递减,后三位数字由小到大排列。
输出后是这样:
新数组:3025,2007,1022,3062,2077,1052,3064,2129,1084,3066,1123,3070,3101,3133
规则: 各个数字按第一位数出现次数由多到少,第一位数递增或递减,后三位数字由小到大排列。
输出后是这样:
新数组:3025,2007,1022,3062,2077,1052,3064,2129,1084,3066,1123,3070,3101,3133
但是排序可以直接用Sort方法, 不是可以传委托进去么。只要写比较规则就行了。
{
int[] num = new int[] { 1022, 1052, 3025, 3064, 3066, 3070, 3062, 3101, 3133, 1084, 1123, 2007, 2077, 2129 }; int[] num1 = new int[num.Length];
int[] num2 = new int[num.Length];
int[] num3 = new int[num.Length];
int j = 0, y = 0, n = 0;
for (int i = 0; i < num.Length; i++)
{
int um = int.Parse(num[i].ToString().Substring(0, 1));
if (um == 1)
{
num1[j] = num[i];
j++;
}
if (um == 2)
{
num2[n] = num[i];
n++;
}
if (um == 3)
{
num3[y] = num[i];
y++;
}
}
ArrayList list = new ArrayList();
for (int p = 0; p < num.Length; p++)
{
if (num3[p] != 0)
{
list.Add(num3[p]);
}
if (num2.Length > p && num2[p] != 0)
list.Add(num2[p]);
if (num1.Length > p && num1[p] != 0)
list.Add(num1[p]); }
for (int m = 0; m < list.Count; m++)
Console.Write("{0} ", list[m]); } private static void Sort(int[] list)
{
for (int i = 0; i < list.Length - 1; i++)
{
int um = int.Parse(list[i].ToString().Substring(0, 1));
if (um == 3)
{
for (int j = 0; j < list.Length - 1 - i; j++)
{
if (list[j] > list[j + 1])
{
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
} }
希望来高手能帮你解决