我定义了如下的struct资料        public struct people
        {
            int lv;
            int no;
        }
        people[] mypeople = new people[6];我的数据排序前是
lv   no7    4
2    2
2    1
4    3
1    6
1    5
我希望按lv为第一优先排序,再次按no为第二优先排序,排序完结果如下:
lv    no
1     5
1     6
2     1
2     2
4     3
7     4请问该怎么做?谢谢

解决方案 »

  1.   

    我试过sort或是icompareble,但是只能根据no或是lv来排序一次…请问要怎么做才能排序两次呢?
      

  2.   


    SQL数据库中去弄这个吧!SELECT * FROM TB ORDER BY LV ASC ,NO ASC
    SELECT * FROM TB ORDER BY 1,2
      

  3.   


    你IComparable实现错了。
    这种要求排序一次就可以了。
      

  4.   

    先用for对lv排序,再在lv范围内,按no输出。
      

  5.   

    我自己的详细代码如下,但是只能对lv排序,请问要怎么做才能先对lv排序再对no排序,请四方大侠指教        public struct people : IComparable<people>
            {
                public int lv;
                public int no;
                public int CompareTo(people p)
                {
                    return this.lv.CompareTo(p.lv);
                }
            }
                List<people> People = new List<people>();
                people[] mypeople = new people[6];
                mypeople[0].lv = 7; mypeople[0].no = 4;
                mypeople[1].lv = 2; mypeople[1].no = 2;
                mypeople[2].lv = 2; mypeople[2].no = 1;
                mypeople[3].lv = 4; mypeople[3].no = 3;
                mypeople[4].lv = 1; mypeople[4].no = 6;
                mypeople[5].lv = 1; mypeople[5].no = 5;
                for (int i = 0; i < mypeople.Length; i++)
                {
                    People.Add(mypeople[i]);
                }
                People.Sort();//这里只有计对lv排序,请问要如何也对no排序?
                for (int i = 0; i < People.Count; i++)
                {
                    richTextBox1.AppendText("lv= " + People[i].lv + ",no= " + People[i].no + "\r\n");
                }
      

  6.   

    try,try!
        
        public class People
        {
            int lv = 0;
            int lo = 0;        public static void SortPeoples(People[] peoples)
            {
                Array.Sort<People>(peoples, new Comparison<People>(ComparePeople1));//比较方法1
                Array.Sort<People>(peoples, new Comparison<People>(ComparePeople2));//比较方法2
            }        //比较方法1
            private static int ComparePeople1(People p1, People p2)
            {
                if (p1.lv == p2.lo) return CompareInt(p1.lo, p2.lo);
                return CompareInt(p1.lv, p2.lv);
            }        private static int CompareInt(int n1, int n2)
            {
               if  (n1 > n2)  return 1 ;
               if  (n1 < n2)  return -1 ;
                return 0;
            }        //比较方法2
            private static int ComparePeople2(People p1, People p2)
            {
                string key1 = p1.lv.ToString().PadLeft(sizeof(Int32)) + p1.lo.ToString().PadLeft(sizeof(Int32));
                string key2 = p2.l.ToString().PadLeft(sizeof(Int32)) + p2.lo.ToString().PadLeft(sizeof(Int32));
                return string.Compare(key1, key2);
            }
        }
      

  7.   

       
    改正:   public class People
        {
            int lv = 0;
            int lo = 0;        public static void SortPeoples(People[] peoples)
            {
                Array.Sort<People>(peoples, new Comparison<People>(ComparePeople1));//比较方法1
                Array.Sort<People>(peoples, new Comparison<People>(ComparePeople2));//比较方法2
            }        //比较方法1
            private static int ComparePeople1(People p1, People p2)
            {
                if (p1.lv == p2.lv) return CompareInt(p1.lo, p2.lo);//改正
                return CompareInt(p1.lv, p2.lv);
            }        private static int CompareInt(int n1, int n2)
            {
               if  (n1 > n2)  return 1 ;
               if  (n1 < n2)  return -1 ;
                return 0;
            }        //比较方法2
            private static int ComparePeople2(People p1, People p2)
            {
                string key1 = p1.lv.ToString().PadLeft(sizeof(Int32)) + p1.lo.ToString().PadLeft(sizeof(Int32));
                string key2 = p2.l.ToString().PadLeft(sizeof(Int32)) + p2.lo.ToString().PadLeft(sizeof(Int32));
                return string.Compare(key1, key2);
            }
        }