Better way to sort array in descending order

后端 未结 7 2187
伪装坚强ぢ
伪装坚强ぢ 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

    For in-place sorting in descending order:

    int[] numbers = { 1, 2, 3 };
    Array.Sort(numbers, (a, b) => b - a);
    

    For out-of-place sorting (no changes to input array):

    int[] numbers = { 1, 2, 3 };
    var sortedNumbers = numbers.OrderByDescending(x => x).ToArray();
    

提交回复
热议问题