Better way to sort array in descending order

后端 未结 7 2194
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 09:54

I have a array of int which I have to sort by descending.

Since I did not find any method to sort the array in descending order.Currently I am sorting the array in

7条回答
  •  天涯浪人
    2020-12-01 11:00

    Sure, You can customize the sort.

    You need to give the Sort() a delegate to a comparison method which it will use to sort.

    Using an anonymous method:

    Array.Sort( array,
    delegate(int a, int b)
      {
        return b - a; //Normal compare is a-b
      }); 
    

    Read more about it:

    Sorting arrays
    MSDN - Array.Sort Method (T[], Comparison)

提交回复
热议问题