Summary#
list.OrderBy(x => x.Key) sorts a local List<T> by the selected key and returns a new sorted
List<T> — the source list is left unchanged, exactly like C#'s Enumerable.OrderBy(...).
OrderByDescending sorts in reverse. The sort is stable: elements with equal keys keep their input
order.
Signature#
list.OrderBy(<element> x => <key>) // ascending → a new List<T>
list.OrderByDescending(<element> x => <key>) // descending → a new List<T>Description#
The receiver is a List<T> (new List<T>() or a Text.Split result). The key selector x => x.Key
projects each element to a comparable value (a number, string, date, …); a non-comparable key is a
compile error. The result is itself a List<T>, so it can be iterated, indexed
(sorted[0], see List indexer), counted, and sorted again.
This is an in-memory sort with no database dependency — the twin of an entity-query OrderBy, which
instead lowers to SQL ORDER BY. Use it to rank locally-built lists (merge results, computed scores).
Single-key only for now; ThenBy is not yet available.
list.Take(n) returns a new List<T> of the first n elements (in memory). n is any integer and is
clamped like C# — n larger than the list keeps all elements, n <= 0 yields an empty list, and it
never throws. It chains after OrderBy for top-k ranking: items.OrderByDescending(k).Take(3).
Examples#
class Scored { public string Id; public decimal Score; }
List<Scored> Top3(List<Scored> items) {
return items.OrderByDescending(s => s.Score).Take(3); // top 3 by score; input order kept on ties
}See also#
- List indexer — positional access on the sorted result
- Text.Split — produces a
List<string>you can sort