C# List<>.sort 排序【转载】

转载:http://hi.baidu.com/lifangfang1218/blog/item/aa49002362f76bf9d6cae244.html

第三种方法需要编写一个对象排序比较的方法,对List中的元素对象没有特殊的要求,但在比较方法中需要实现对象比较规则,这个方法实现后,就可以把这方 名字作为参数委托给List的Sort方法,Sort方法在排序时会执行这个方法对List中的对象进行比较,详细可参照下面的代码。对List中元素我 们还使用上面的SortTestObj2类对象。

  1.         staticvoid Main(string[] args)
  2.          {
  3.             //利用代理方法进行排序
  4.              DelegateSort();
  5.          }
  6.         
  7.         //Sort(Comparison<(Of <(T>)>))方法排序,这中方法需要先编写一个对象比较的方法,然后
  8.         //把这个比较方法委托给List的Sort方法。
  9.         //对象比较的方法
  10.         privatestaticint SortTestObj2Compare(SortTestObj2 obj1, SortTestObj2 obj2)
  11.          {
  12.             int res = 0;
  13.             if ((obj1 == null) && (obj2 == null))
  14.              {
  15.                 return 0;
  16.              }
  17.             elseif ((obj1 != null) && (obj2 == null))
  18.              {
  19.                 return 1;
  20.              }
  21.             elseif ((obj1 == null) && (obj2 != null))
  22.              {
  23.                 return -1;
  24.              }
  25.             if (obj1.Code > obj2.Code)
  26.              {
  27.                  res = 1;
  28.              }
  29.             elseif (obj1.Code < obj2.Code)
  30.              {
  31.                  res = -1;
  32.              }
  33.       //如果是时间类型的比较,我现在写的是倒序,如果想正序把1改为-1,-1改为1
  34.        if (DateTime.Parse(obj1.MailDate).CompareTo(DateTime.Parse(obj2.MailDate))>0)
                {
                res = -1;
                    }
                else if (DateTime.Parse(obj1.MailDate).CompareTo(DateTime.Parse(obj2.MailDate)) < 0)
                {
                res = 1;
                 }
  35.        
  36.    //如果是string类型的比较,我现在写的是倒序,如果想正序把1改为-1,-1改为1
  37. //if (obj1.MailDate.CompareTo(obj2.MailDate) > 0)
                //    {
                //    res = -1;
                //    }
                //else if (obj1.MailDate.CompareTo(obj2.MailDate) < 0)
                //    {
                //    res = 1;
                //    }
            
  38.             return res;
  39.          }
  40.         //List的委托排序
  41.         privatestaticvoid DelegateSort()
  42.          {
  43.              List<SortTestObj2> objLst = new List<SortTestObj2>();
  44.              SortTestObj2 obj1 = new SortTestObj2();
  45.              obj1.Code = 3;
  46.              obj1.Name = "TestObj1";
  47.              objLst.Add(obj1);
  48.              SortTestObj2 obj2 = new SortTestObj2();
  49.              obj2.Code = 2;
  50.              obj2.Name = "TestObj2";
  51.              objLst.Add(obj2);
  52.              SortTestObj2 obj3 = new SortTestObj2();
  53.              obj3.Code = 4;
  54.              obj3.Name = "TestObj4";
  55.              objLst.Add(obj3);
  56.              SortTestObj2 obj4 = new SortTestObj2();
  57.              obj4.Code = 1;
  58.              obj4.Name = "TestObj3";
  59.              objLst.Add(obj4);
  60.              SortTestObj2 obj5 = new SortTestObj2();
  61.              obj5.Code = 6;
  62.              obj5.Name = "TestObj6";
  63.              objLst.Add(obj5);
  64.              objLst.Sort(SortTestObj2Compare);
  65.              Console.WriteLine("委托方法排序的结果");
  66.             foreach (SortTestObj2 item in objLst)
  67.              {
  68.                  Console.WriteLine("Code=" + item.Code + ",Name=" + item.Name);
  69.              }
  70.          }
posted @ 2012-02-17 09:31  zp_Alex  阅读(558)  评论(0编辑  收藏  举报