一个数组其中有若干个5,10,13如下:
5,10,10,13,13,13,5,5,5,5,
我想让它按5,10,13这样排序,其余按顺序排,如下
5,10,13,5,10,13,5,5,5,13,不知用什么方法这样排序?

解决方案 »

  1.   

    不用排序的做法           
                int[] num = {5,10,10,13,13,13,5,5,5,5};
                int count5 = 0;
                int count10 = 0;
                int count13 = 0;
                for (int i = 0; i < num.Length; i++)
                {
                    if (num[i] == 5) count5++;
                    else if (num[i] == 10) count10++;
                    else if (num[i] == 13) count13++;
                }
                int count = 0;
                count = count5 < count10 ? count5 : count10;
                count = count < count13 ? count : count13;
                for (int i = 0; i < count; i++)
                {
                    Console.Write("5,10,13,");
                }
                for (int i = count; i < count5; i++)
                {
                    Console.Write("5,");
                }
                for (int i = count; i < count10; i++)
                {
                    Console.Write("10,");
                }
                for (int i = count; i < count13; i++)
                {
                    Console.Write("13,");
                }
      

  2.   

    先统计5的总数,10的总数,13的总数,选择最小的那个总数,也就是有多少个5,10,13了,每个总数减去最小的那个总数,剩下的从小到大排序出来就行了,比如说:int[] a ={5,10,10,13,13,13,5,5,5,5};有2个10,5个5,3个13,那么b[0]到b[5]就是5,10,13,5,10,13,剩下3个5,b[6]到b[8]就为5,1个13,是b[9]
      

  3.   

    参考,不过效率不是很高:            int[] arr = { 5, 10, 10, 13, 13, 13, 5, 5, 5, 5 };
                List<int> list = new List<int>();
                int index = -1;
                while (list.Count < arr.Length)
                {
                    switch (list.Count % 3)
                    {
                        case 0:
                            index = Array.IndexOf(arr, 5);
                            break;
                        case 1:
                            index = Array.IndexOf(arr, 10);
                            break;
                        case 2:
                            index = Array.IndexOf(arr, 13);
                            break;
                    }
                    if (index != -1)
                    {
                        list.Add(arr[index]);
                        arr[index] = -1;
                    }
                    else
                        break;
                }
                int[] temp = Array.FindAll(arr, i => i != -1);
                Array.Sort(temp);
                list.AddRange(temp);
                arr = list.ToArray();
                foreach (int i in arr)
                    Console.WriteLine(i);
      

  4.   

    如果楼主用的是vs2005,把 int[] temp = Array.FindAll(arr, i => i != -1);
    改为 int[] temp = Array.FindAll(arr, delegate(int i) { return i != -1; });
      

  5.   

    楼上的众多方法都可以实现,不过应该关注的是怎么样效率才能最高吧
    我有个方法供参考:
    新建一数组A[3*B排列数组]和3个变量c5=0,c10=1,c13=2:内容为3个元素为1、2、3、1、2、3......;
    然后循环B排列数组,如果5则A[c5]=5且c5+=3,如果10则A[c10]=10且c10+=3,
    如果13则A[c13]=13且c13+=3,以此类推,循环数组次;
    最后把1、2、3元素删掉就OK了或是输出时不显示1、2、3。
      

  6.   

    7楼和10楼的方法不错,是我没表达清楚。
    其实是一个扑克的小程序,可能大家都玩过五十K这种扑克玩法,两付牌四种
    花色,黑桃、方块、梅花、红桃也就是每种花色两张牌,为方便编程用1代表
    黑桃,105就代表黑桃5,110代表黑桃10,113代表黑桃K,以此类推
    用2代表红桃,205就代表红桃5,210代表红桃10,213代表红桃K
    3代表梅花,305,310,313分别代表梅花5,10,K
    4代表方块,405,410,413分别代表方块5,10,K,
    这样分到手的36张牌中有不同花色的5,10,K,
    同色的放在一起,如都是红桃的5,10,K;如果没有同色的,可以组成5,10,K的
    也放在一起,如红桃5,方块10,红桃K,剩下的按顺序排列。我的程序是这样的
     for (int i = 0; i < 36; i++)
              if (PokerA[theArray[i]].number % 100 == 5 || PokerA[theArray[i]].number % 100 == 10 || PokerA[theArray[i]].number % 100 == 13)
                    {
                      if (i != n)
                        {
                            p = theArray[i];
                            theArray[i] = theArray[n];
                            theArray[n] = p;
                            
                        }
                       PokerA[theArray[n]].wsk = 1;
                       n++;
                    }  //PokerA[theArray[i]].number % 100 == 5数组的值除5取余,这段是将36张牌中不同花色的5,10,K,放在0——n位。
               
                for (int i = 0; i < n ; i++)
                {
                    for (m = i, j = i + 1; j < n ; j++)
                        if (PokerA[theArray[m]].number > PokerA[theArray[j]].number)                        m = j;
                    if (m != i)
                    {
                        p = theArray[i];
                        theArray[i] = theArray[m];
                        theArray[m] = p;                }
                } //0--n位排序。这样排可以将同色的放在一起,但杂色的就不行了,我是想如果能组成同色的放在一起组成5,10,K,不成杂色的能组成5,10,K也放在一起
    剩下的按顺序排。
      

  7.   

    这样前面的数组有可能是这样的
    105,110,210,113,413,313,105,205,205,405
    排序后可以是这样
    105,110,113,105,210,313,205,205,405,413
    也就是一组同色黑桃5,10,K,一组杂色5,10,K,剩下三个5,一个K;
      

  8.   

    数据库里面排倒是很简单 charindex