List.sort() 默認的情況下是從小到大的排列。
例如:
           List<int> list = new List<int>(); 
          
list.Add(-2);
list.Add(10);
list.Add(8);
list.Add(6);
list.Add(4);
list.Sort();
 
          
        list.Add(-2);
list.Add(10);
list.Add(8);
list.Add(6);
list.Add(4);
list.Sort();
這時候的輸出結果是 -2,4,6,8,10;
如果,要將list中的數據從大到小怎么排列呢?這將怎么實現呢。請看下面代碼:
           1 list.Sort( 
          
2 delegate(int a, int b)
3 {
4 return a.CompareTo(b);
5 }
6 );
7
8
9 list.Sort((a, b) =>b.CompareTo(a)); //lamda expression
 
          
        2 delegate(int a, int b)
3 {
4 return a.CompareTo(b);
5 }
6 );
7
8
9 list.Sort((a, b) =>b.CompareTo(a)); //lamda expression
在代理中1--6行代碼中,是從小到大排列的,第9行 只要將后面的參數與前面的參數進行對比,這樣返回的結果,將是一個從大到小排列了。這種方式適合於字符串等許多類型的排序。
和大家分享一下 :)

