C# OrderByDescending: Sort in Reverse With LambdaUse OrderByDescending from System.Linq. A lambda argument returns the sorted value.
This sorts elements from high to low. It is an extension method found in the System.Linq namespace. It receives a key selector Func instance. We pass it a lambda expression that selects the key.FuncExtension
Let us first create an array of Tuples. You can see that each Tuple has 2 items of type int. Next, we call OrderByDescending and use a lambda expression that selects the Item1 property of each Tuple instance.
Finally: We see that the Tuples are sorted from the highest Item1 value to the lowest Item1 value.
Info: A reverse sort (a descending sort) is performed. Using descending sorts is usually faster than using a separate reverse pass.
C# program that uses OrderByDescending
static void Main()
// Create array of tuples.
Tuple<int, int> tuples = new Tuple<int, int>;
tuples = new Tuple<int, int>(3, 6);
tuples = new Tuple<int, int>(6, 4);
tuples = new Tuple<int, int>(0, 60);
// Order by descending on Item1.
var result = tuples.OrderByDescending(a => a.Item1);
foreach (var item in result)
This extension method
is useful in some situations. But it is probably most often used by the C# compiler when it translates query expressions with the descending contextual keyword.Descending
Tip: Because query expressions do not require explicit delegate syntax (such as lambda expressions =>) they can be easier to read.
I checked the C# specification for details about the descending keyword in queries. When we use "descending," a call of OrderByDescending is added to the translated, method-call syntax.
Quote: If an ordering clause specifies a descending direction indicator, an invocation of OrderByDescending or ThenByDescending is produced instead (The C# Programming Language).
OrderByDescending, a method, is sometimes useful. It requires a Func instance that selects a key. This method is commonly used through the translation of query expressions with the orderby and descending keywords.OrderBy Clause
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.