OrderBy vs ThenBy vs Reverse in C# LINQ
Sorting collections in C# using Language Integrated Query (LINQ)
relies on specific methods designed for primary ordering, secondary
ordering, and directional inversion. This article explains the technical
and practical differences between OrderBy,
ThenBy, and reverse sorting methods—including
OrderByDescending, ThenByDescending, and
Reverse()—demonstrating how each method alters data
sequences and how to avoid common sorting mistakes in C#.
OrderBy and OrderByDescending: Primary Sorting
OrderBy and OrderByDescending establish the
primary sort order for an IEnumerable<T>. When you
call OrderBy, LINQ evaluates the specified key selector
function and reorganizes the collection in ascending order (smallest to
largest or alphabetical A-Z). OrderByDescending works
identically, but sorts in descending order (largest to smallest or
Z-A).
Both methods return an instance of
IOrderedEnumerable<T>, which preserves the sorting
state and allows subsequent sort criteria to be chained.
var sortedByAge = people.OrderBy(p => p.Age);
var sortedByAgeDesc = people.OrderByDescending(p => p.Age);Calling OrderBy or OrderByDescending a
second time on an already sorted collection completely overrides the
first sort rather than refining it.
ThenBy and ThenByDescending: Secondary Sorting
ThenBy and ThenByDescending provide
secondary, tertiary, or further sorting criteria. These methods can only
be invoked on an IOrderedEnumerable<T> returned by
OrderBy, OrderByDescending, or another
ThenBy method.
ThenBy does not alter the primary groupings established
by OrderBy. Instead, it sorts elements only when there are
ties in the primary key:
// Sorts by LastName first. If LastNames are identical, sorts by FirstName.
var sortedPeople = people
.OrderBy(p => p.LastName)
.ThenBy(p => p.FirstName);If you mistakenly use OrderBy instead of
ThenBy for the second property, the original ordering is
lost:
// INCORRECT: The OrderBy on FirstName completely overrides the LastName sort.
var badSort = people
.OrderBy(p => p.LastName)
.OrderBy(p => p.FirstName);Reverse Ordering: Descending Methods vs. Reverse()
Reverse ordering in LINQ can mean two distinct operations: sorting data in descending key order, or inverting the physical position of items in an existing sequence.
1.
Key-Based Reverse Sorting (OrderByDescending /
ThenByDescending)
These methods sort elements using a key selector, comparing values
according to an IComparer<T> to arrange them from
highest to lowest:
var sorted = people
.OrderBy(p => p.Department)
.ThenByDescending(p => p.Salary);2. Positional Reversal
(Reverse())
The Reverse() method does not sort by values or evaluate
keys. It simply inverts the current sequence order, turning the first
element into the last and the last into the first:
var numbers = new[] { 5, 1, 9, 3 };
var reversed = numbers.Reverse(); // Result: 3, 9, 1, 5Summary Comparison
OrderBy/OrderByDescending: Starts a sorting operation. Replaces any prior sort and returnsIOrderedEnumerable<T>.ThenBy/ThenByDescending: Continues a sorting operation. Resolves ties without disrupting prior sort tiers; requires a precedingOrderByorThenBy.Reverse(): Inverts the index-based order of an existing sequence without evaluating element properties or sorting keys.