Summary: in this tutorial, you’ll learn how to use the LINQ ThenByDescending()
method to sort a sequence in descending order based on a secondary key.
Introduction to the LINQ ThenByDescending() method
The LINQ ThenByDescending()
method sorts a sequence in descending order based on a secondary key after the sequence has been sorted by a primary key using either OrderBy()
or OrderByDescending()
method.
public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey> (
this IOrderedEnumerable<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 sort key.source
is the input sequence to sort.keySelector
is a function that selects a key to sort thesource
sequence.
The ThenByDescending()
method returns a new sequence sorted by a key in descending order. The type of resulting sequence is an IOrderedEnumerable<TElement>
.
If the source or keySelector
is null, the method ThenByDescending()
method will throw an ArgumentNullException
.
LINQ ThenByDescending() method example
The following program uses the OrderByDesending()
method to sort a list of employees by departments in descending order first and then uses the ThenByDescending()
method to sort the employees in each department by Salary
from high to low:
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.Department)
.ThenByDescending(e => e.Salary);
foreach (var e in results)
{
WriteLine($"{e.Department} - {e.Name} - {e.Salary:C}");
}
}
}
Code language: C# (cs)
Output:
IT - Tom - $65,000.00
IT - Jane - $60,000.00
IT - Sara - $55,000.00
HR - John - $50,000.00
HR - Bob - $45,000.00
Code language: C# (cs)
How it works.
First, define the 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, create a new list that has five Employee
objects:
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 Employee
objects by departments in descending order using the OrderByDescendig
method and then sort the Employee
objects in each department by salary using the ThenByDescending()
method:
var results = employees.OrderByDescending(e => e.Department)
.ThenByDescending(e => e.Salary);
Code language: C# (cs)
Finally, write the employees to the console using a foreach
and WriteLine()
method:
foreach (var e in results)
{
WriteLine($"{e.Department} - {e.Name} - {e.Salary:C}");
}
Code language: C# (cs)
Summary
- Use LINQ
ThenByDescending()
method to sort elements in a sequence in descending order by a secondary key, after the sequence has been sorted by a primary key.