Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# LINQ OrderByDescending: Sorting in Descending Order

Learn how to sort collections in descending order with C# LINQ OrderByDescending: syntax, key selectors, chained ThenByDescending sorts, null handling, and performance considerations.

~3 min readC#
#LINQ#C##Sorting#OrderByDescending
A descending arrow over a set of ordered cards representing C# LINQ OrderByDescending sorting.

The OrderByDescending method in C# LINQ sorts a sequence in descending order based on a key selector function. It is the direct counterpart to OrderBy, which sorts ascending, and it is the operator to use when the largest values must appear first.

OrderByDescending Syntax and Basic Usage

The extension method takes a key selector argument and returns an IOrderedEnumerable<T> in LINQ to Objects, which can be enumerated like any IEnumerable<T>.

csharp
using System.Linq; int[] scores = { 42, 17, 93, 55, 71 }; IOrderedEnumerable<int> ranked = scores.OrderByDescending(s => s);

The lambda s => s selects the element itself as the sort key. For a sequence of integers, this produces the order 93, 71, 55, 42, 17. The original array is not modified; OrderByDescending returns a new ordered sequence.

Sorting by a Property with a Key Selector

The key selector is where OrderByDescending becomes useful for real objects. You supply a lambda that returns the property to sort on.

csharp
public record Product(string Name, decimal Price); var products = new[] { new Product("Keyboard", 89.99m), new Product("Mouse", 24.50m), new Product("Monitor", 299.00m) }; var byPriceDescending = products.OrderByDescending(p => p.Price);

Here the sequence is ordered by Price from highest to lowest: Monitor, Keyboard, Mouse. The selector can be any expression that returns a comparable value — a numeric property, a string, a DateTime, or a custom type that implements IComparable<T>.

Secondary Sorting with ThenByDescending

When two elements have the same sort key, OrderByDescending alone leaves their relative order as it appeared in the source. To break ties, chain ThenByDescending after the primary ordering.

csharp
public record Employee(string Name, string Department, int Salary); var employees = new[] { new Employee("Ana", "Engineering", 120000), new Employee("Ben", "Engineering", 120000), new Employee("Cara", "Design", 110000) }; var sorted = employees .OrderByDescending(e => e.Salary) .ThenByDescending(e => e.Name);

The primary sort puts Ana and Ben first because they have the highest salary. The secondary ThenByDescending orders those tied records by name descending, so Ben appears before Ana; Cara is last. ThenBy and ThenByDescending can be chained repeatedly, and each subsequent call only affects elements that were tied by the previous key.

How OrderByDescending Handles Null Values

The default comparer treats null as smaller than any non-null value. In a descending sort, that places null keys at the end of the sequence.

csharp
var items = new[] { "banana", null, "apple", "cherry" }; var sorted = items.OrderByDescending(s => s);

The result is "cherry", "banana", "apple", then null last. If you need nulls first, provide a custom comparer that reverses the default ordering. If you want to exclude nulls, filter them out before sorting with Where(x => x != null).

Performance and Deferred Execution

OrderByDescending returns a lazily evaluated sequence. The sort itself is not performed until the sequence is enumerated — by a foreach loop, ToList(), ToArray(), or another consuming operator.

csharp
var query = products.OrderByDescending(p => p.Price); // No sorting has happened yet var materialized = query.ToList(); // Sorting happens here

In LINQ to Objects, the sort is a stable O(n log n) operation. Stability means elements with equal keys keep their original relative order. The cost is dominated by the key selector being evaluated once per element and by the comparison operations during the sort. For typical in-memory collections this is not a bottleneck, but if you sort a very large sequence repeatedly, consider whether the data could be maintained in sorted order at insertion time instead.

OrderByDescending vs In-Place Sorting

If you already have a List<T> and do not need the original order preserved, List<T>.Sort with a custom comparison can be more efficient because it sorts in place and avoids allocating a new sequence.

csharp
products.Sort((a, b) => b.Price.CompareTo(a.Price));

The tradeoff is that List<T>.Sort is unstable, mutates the collection, and requires writing a comparison delegate. OrderByDescending is the better default for most code because it is declarative, works on any IEnumerable<T>, and leaves the source untouched. Choose in-place sorting only when the list is large, the original order is no longer needed, and allocation matters.

Common Mistakes and Avoiding Them

A frequent error is calling OrderByDescending and then ignoring the returned sequence, expecting the original collection to be sorted. Because the method is not in-place, the original collection remains unchanged.

Another mistake is using OrderByDescending on a property that does not implement IComparable<T> or does not otherwise have a default comparer. In that case, pass an IComparer<T> explicitly:

csharp
var sorted = records.OrderByDescending(r => r.CustomField, new MyComparer());

Finally, remember that OrderByDescending is stable only in LINQ to Objects. If you are using a different LINQ provider, such as Entity Framework, the ordering is translated to the underlying query language and stability is not guaranteed.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.