This post will discuss how to sort an array in descending order in C#.

1. Using Array.Sort method

The standard solution to in-place sort an array in C# is using the Array.Sort() method. It takes a comparator to compare two elements and determine which element should appear first in the final sorted array. For sorting in descending order, we can provide a custom comparison function that reverses the default sort order.

Download  Run Code

 
The above solution uses a lambda expression to create an anonymous function. In older versions of C#, consider using the anonymous method feature of C# with Action<T> delegate.

Download  Run Code

2. Using Enumerable.OrderByDescending method

The LINQ’s Enumerable.OrderByDescending method sorts elements of a sequence in descending order. This method avoids modifications to the original array and returns a new sorted array instead according to the specified comparator. This is the preferred method for out-of-place sorting in C#.

Download  Run Code

That’s all about sorting an array in descending order in C#.