Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Collections

List<T>.Sort()

Reference for C# List<T>.Sort(): default ordering, Comparison lambdas, IComparer, sorting a range, instability and when to use LINQ OrderBy instead.

List<T>.Sort() sorts the list in place. Called without arguments it uses the default ordering of the element type (IComparable<T>), so numbers sort numerically and strings sort by the current culture rules. You can pass a Comparison<T> lambda or an IComparer<T> to define any order you like.

The sort is not stable, which means items that compare as equal may end up in a different relative order than before. If you need stability or want a new sequence instead of changing the list, use LINQ OrderBy.

C#
var names = new List<string> { "Cleo", "Abe", "Bea" };
names.Sort();                                   // Abe, Bea, Cleo
names.Sort((a, b) => b.CompareTo(a));           // descending

Try it

C# Default order and comparisons

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.

C# Custom comparers and errors

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

C#
public void Sort();
public void Sort(Comparison<T> comparison);
public void Sort(IComparer<T>? comparer);
public void Sort(int index, int count, IComparer<T>? comparer);
public void Reverse();
Name Description
comparison A delegate that returns a negative number if the first argument sorts before the second, zero if they are equal, and a positive number otherwise.
comparer An IComparer<T>. null means the default comparer for T.
index, count The range of the list to sort.

Returns: nothing; the list itself is reordered.

Errors: InvalidOperationException if T does not implement IComparable<T> (or IComparable) and no comparer is given, or if the comparer throws. A comparison that is inconsistent (for example, one that never returns zero for equal items) can throw InvalidOperationException too, or produce an unreliable order. ArgumentException for an invalid range.

Usage notes

  • Not stable. Equal items can switch places. OrderBy(...).ThenBy(...) is a stable alternative that returns a new sequence.
  • Complexity: O(n log n) on average using introspective sort, in place, with no big temporary allocation.
  • Default string sorting is culture-aware. Output can differ between machines. Pass StringComparer.Ordinal for predictable ordering, or OrdinalIgnoreCase for case-insensitive.
  • Subtract with care. (a, b) => a - b overflows for large or negative integers. Use a.CompareTo(b).
  • Descending order: swap the arguments in the comparison, or sort ascending and call Reverse().
  • Keep the comparison consistent: if Compare(a, b) is negative then Compare(b, a) must be positive.
  • Sorted data enables BinarySearch. Use the same comparer for sorting and searching.
  • Arrays: Array.Sort(array) is the counterpart, and span.Sort() sorts a Span<T> in place.
  • Keep sorting cheap: for repeated insertion into a sorted collection, use SortedSet<T> or SortedDictionary<TKey,TValue> instead of re-sorting a list.