Summary: in this tutorial, you’ll learn how to use the LINQ OrderByDescending()
method to sort elements of a sequence in descending order based on a specified key.
Introduction to the LINQ OrderByDescending() method
The OrderByDescending()
method sorts elements of a sequence in descending order and returns a new sorted sequence.
The following shows the syntax of the OrderByDescending()
method:
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey> (
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
);
Code language: C# (cs)
In this syntax:
TSource
is the type of elements in thesource
sequence.TKey
is the type of key returned by thekeySelector
function.source
is the sequence of elements to sort, which is anIEnumerable<T>
type.keySelector
is a function to extract a key from an element.
The OrderByDescending()
method returns an IOrderedEnumerable<TElement
> that contains elements sorted in descending according to a key.
If the source
sequence or keySelector
function is null
, the method throws an ArgumentNullException
.
To sort a sequence of elements by a key in ascending order, you use the OrderBy()
method instead.
LINQ OrderByDescending() method examples
Let’s take some examples of using the OrderByDescending()
method.
1) Using the OrderByDescending() method to sort a sequence of numbers
The following example demonstrates how to use the OrderByDescending()
method to sort a sequence of numbers in descending order:
using static System.Console;
int[] numbers = { 3, 1, 4, 0, 5 };
var sortedNumbers = numbers.OrderByDescending(number => number);
foreach (var number in sortedNumbers)
{
Write($"{number} ");
}
Code language: C# (cs)
Output:
5 4 3 1 0
Code language: C# (cs)
How it works.
First, declare an array of integers and initialize it with five numbers:
int[] numbers = { 3, 1, 4, 0, 5 };
Second, sort the numbers in the numbers array in descending order using the OrderByDescending()
method. The lambda expression number => number
returns the number in the numbers array for sorting:
var sortedNumbers = numbers.OrderByDescending(number => number);
Code language: JavaScript (javascript)
Third, write the number in the sequence to the console using a foreach
loop and Write()
method:
foreach (var number in sortedNumbers)
{
Write($"{number} ");
}
Code language: PHP (php)
2) Using the OrderByDescending() method to sort a sequence of strings
The following example uses the OrderByDescending()
method to sort an array of strings in descending order:
using static System.Console;
string[] mountains = {
"Mount Everest",
"K2",
"Kangchenjunga",
"Lhotse",
"Makalu"
};
var sortedMountains = mountains.OrderByDescending(m => m);
foreach (var mountain in sortedMountains)
{
WriteLine(mountain);
}
Code language: C# (cs)
Output:
Mount Everest
Makalu
Lhotse
Kangchenjunga
K2
Code language: C# (cs)
3) Using LINQ OrderByDescending() method to sort a sequence of objects in descending order according to a key
The following program demonstrates how to use the OrderByDescending()
method to sort a list of Employee objects by salary in descending order:
using static System.Console;
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}
class Program
{
public static void Main()
{
var employees = new List<Employee>()
{
new Employee { Name = "John", Department = "HR", Salary = 50000 },
new Employee { Name = "Jane", Department = "IT", Salary = 60000 },
new Employee { Name = "Bob", Department = "HR", Salary = 45000 },
new Employee { Name = "Sara", Department = "IT", Salary = 55000 },
new Employee { Name = "Tom", Department = "IT", Salary = 65000 }
};
var results = employees.OrderByDescending(e => e.Salary);
foreach (var employee in results)
{
WriteLine($"{employee.Name} - {employee.Salary:C}");
}
}
}
Code language: C# (cs)
Output:
Tom - $65,000.00
Jane - $60,000.00
Sara - $55,000.00
John - $50,000.00
Bob - $45,000.00
Code language: C# (cs)
How it works.
First, define an Employee
class that has three properties: Name
, Department
, and Salary
.
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}
Code language: C# (cs)
Second, define a list of Employee
objects in the Main()
method:
var employees = new List<Employee>()
{
new Employee { Name = "John", Department = "HR", Salary = 50000 },
new Employee { Name = "Jane", Department = "IT", Salary = 60000 },
new Employee { Name = "Bob", Department = "HR", Salary = 45000 },
new Employee { Name = "Sara", Department = "IT", Salary = 55000 },
new Employee { Name = "Tom", Department = "IT", Salary = 65000 }
};
Code language: C# (cs)
Third, sort the employees
list by salary in descending order using the OrderByDescending()
method:
var results = employees.OrderByDescending(e => e.Salary);
Code language: C# (cs)
The lambda expression e => e.Salary
extracts the Salary
value of each Employee
object for sorting.
Finally, write the employee information to the console using a foreach
loop and WriteLine()
method:
foreach (var employee in results)
{
WriteLine($"{employee.Name} - {employee.Salary:C}");
}
Code language: C# (cs)
Summary
- Use the LINQ
OrderByDescending()
method to sort elements of a sequence by a key in descending order.