C# Reference ยท LINQ
LINQ Reverse() and Order() methods
Reference for LINQ Reverse(), Order() and OrderDescending() in C#: flip a sequence, sort simple values without a key selector and avoid span pitfalls.
Reverse() returns the elements of a sequence in the opposite order. Order() and OrderDescending() (added in .NET 7) sort a sequence by the elements themselves, without the key selector that OrderBy(x => x) needs. Together they cover the common one-liners: "newest first", "smallest to largest", "just flip it".
Reverse() buffers the whole source, so it needs a finite sequence and reads it completely before yielding the first result.
var flipped = numbers.Reverse(); var sorted = numbers.Order(); var biggestFirst = numbers.OrderDescending();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static IEnumerable<TSource> Reverse<TSource>( this IEnumerable<TSource> source); public static IOrderedEnumerable<T> Order<T>( this IEnumerable<T> source); public static IOrderedEnumerable<T> Order<T>( this IEnumerable<T> source, IComparer<T>? comparer); public static IOrderedEnumerable<T> OrderDescending<T>( this IEnumerable<T> source); public static IOrderedEnumerable<T> OrderDescending<T>( this IEnumerable<T> source, IComparer<T>? comparer);
| Name | Description |
|---|---|
source |
The sequence to reverse or sort. |
comparer |
Defines the ordering. null or omitted uses Comparer<T>.Default. |
Returns: Reverse() returns a sequence in opposite order. Order() and OrderDescending() return an IOrderedEnumerable<T>, so ThenBy() can follow.
Errors: ArgumentNullException for a null source. Sorting throws InvalidOperationException if elements do not implement IComparable and no comparer is given.
Usage notes
- Deferred but buffering. Nothing runs until you enumerate, but then the whole source is read into memory first.
- Stable sorts.
Order()andOrderDescending()keep the original relative order of equal elements. - Arrays are not changed. On this compiler (C# 14, .NET 10)
array.Reverse()returns a lazy reversed sequence and leaves the array alone. To reverse an array in place, useArray.Reverse(array)orarray.AsSpan().Reverse(). List<T>.Reverse()is different. The instance method reverses the list in place and returnsvoid. To get a reversed copy without touching the list, uselist.AsEnumerable().Reverse()orEnumerable.Reverse(list).- Text order.
Order()on strings uses the default comparer, which depends on culture. It is not a pure ordinal order in general, so passStringComparer.Ordinalwhen you need a predictable code-unit order. - Reversing a string by characters breaks surrogate pairs and combining marks. Use text-element enumeration for user-visible text.
- Cost. O(n) memory for
Reverse(); O(n log n) for sorting. - Availability.
Order()andOrderDescending()require .NET 7 or later.