Osy#betaRun it on Osyrin: osyrin.com

Reference / Function

List Sort, Reverse and RemoveAt (in place)

list.Sort() / list.Reverse() / list.RemoveAt(i) → nothing; the list itself changes

Reorder or shorten a local List<T> IN PLACE, changing the list you are holding rather than answering a new one. Sort() orders by the elements themselves, Reverse() flips the order, RemoveAt(i) deletes by position. The copying counterpart is OrderBy, which leaves the source untouched.

stable2 examples compiled by CIfunctioncollectionmutation

Summary#

list.Sort(), list.Reverse() and list.RemoveAt(i) change the list you are holding. They answer nothing, exactly as their C# counterparts do. That is the whole difference from List OrderBy (in-memory), which answers a new list and leaves the source as it was.

Reach for the in-place verb when anything else is holding the same list — a component member, a value captured in a closure, a list handed to a helper. Those holders see the change. The rewrite people use when they cannot say Sort() — xs = xs.OrderBy(k).ToList() — rebinds the name, so every other holder goes on reading the old order.

Signature#

list.Sort();          // order the list by its elements  → nothing
list.Reverse();       // flip the order                  → nothing
list.RemoveAt(i);     // delete the element at position i → nothing

Description#

All three are List<T> verbs. An array (T[]) is fixed-size and has none of them, which is C#'s rule too — the refusal names List<T> and OrderBy so the fix is one edit.

Sort() orders by the elements themselves, so the element type has to be a comparable scalar — a number, string, date or enum. A list of class values has no natural order, and that is a compile error naming the fix rather than a fault at run time. Sort by a field with OrderBy(x => x.Field).

The order is the one OrderBy uses, so the two verbs never disagree about what the order is; they differ only in whether your list moved. It is ordinal, by code point — not culture-aware, unlike C#'s own List<string>.Sort() — because an order that depends on the machine's locale is not one every engine can promise. Equal elements keep their written order (a stable sort).

Sorting by a rule of your own is list.Sort((a, b) => …) — a comparison answering an int, for an order with no single key (version numbers) — see List Sort with a comparison. When the order is a key, OrderBy(x => key) says it more plainly. IComparer<T> has no spelling here.

Reverse() on a List<T> reverses in place. Inside a LINQ chain the same word still means the sequence verb that answers a reversed copy (list.Reverse().Take(2)), and on an array it always does — which is C#'s own split between the instance method and the extension.

RemoveAt(i) deletes by position; Remove(x) deletes by value and RemoveAll(x => …) deletes every match. An index outside the list throws, as in C#.

Examples#

List<int> Ranked() {
  var scores = new List<int>();
  scores.Add(30);
  scores.Add(4);
  scores.Add(100);

  var alsoScores = scores;   // a second name for the SAME list
  scores.Sort();             // 4, 30, 100 — and `alsoScores` sees it, because nothing was copied

  alsoScores.RemoveAt(0);    // drop the lowest
  alsoScores.Reverse();      // 100, 30
  return scores;
}
class Entry { public string Name; public int Score; }

List<Entry> ByScore(List<Entry> entries) {
  // `Sort()` would be refused here: an Entry has no natural order. Name the key instead.
  return entries.OrderByDescending(e => e.Score).ToList();
}

See also#

Related

List Sort with a comparison

Order a local List<T> IN PLACE by a comparison you write: a lambda taking two elements and answering an int — negative…

List OrderBy (in-memory)

Sort a local List<T> in memory by a key selector, returning a NEW sorted List<T> (the source is untouched). Ascending…

List indexer

Positional get/set on a List<T> by integer index, exactly like C#'s List<T>.this[int]. The index must be an integer…

LINQ over a local list

Query a local `List<T>`, `HashSet<T>` or `T[]` — of your own `class` values OR of plain scalars like `string[]` and…

Sequence fields on a class

A class can hold many values in one field — `string[]`, `int[]`, `List<Tag>`, `HashSet<string>`, `Dictionary<string…