C# – Better way to sort array in descending order

clinqsorting

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 descending order as below

int[] array = new int[] { 3, 1, 4, 5, 2 };
Array.Sort<int>( array );
Array.Reverse( array );

Now,the question is that.Is there any better way to do the same in c#?

Best Answer

Use LINQ OrderByDescending method. It returns IOrderedIEnumerable<int>, which you can convert back to Array if you need so. Generally, List<>s are more functional then Arrays.

array = array.OrderByDescending(c => c).ToArray();