Better way to sort array in descending order

后端 未结 7 2193
伪装坚强ぢ
伪装坚强ぢ 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 10:53

    Depending on the sort order, you can do this :

        int[] array = new int[] { 3, 1, 4, 5, 2 };
        Array.Sort(array,
                        new Comparison(
                                (i1, i2) => i2.CompareTo(i1)
                        ));
    

    ... or this :

        int[] array = new int[] { 3, 1, 4, 5, 2 };
        Array.Sort(array,
                        new Comparison(
                                (i1, i2) => i1.CompareTo(i2)
                        ));
    

    i1 and i2 are just reversed.

提交回复
热议问题