Osy#the first language built for agents
Agents firstAgentic appsWorkflowsDurable Execution — built inSecurityTestingThe editorThe UI modelOne program

Reference / Function

List OrderBy (in-memory)

list.OrderBy(x => x.Key) / OrderByDescending(...) / Take(n) → a new List<T>

Sort a local List<T> in memory by a key selector, returning a NEW sorted List<T> (the source is untouched). Ascending or descending, stable, exactly like C#'s Enumerable.OrderBy; chain Take(n) for top-k. Distinct from an entity-query OrderBy, which lowers to SQL.

stable1 example compiled by CIfunctioncollectionlinq

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#

Related

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…

Text.Split

The C# string.Split: breaks a string on a separator and returns the substrings as a List<string> — iterable with…

Sum / Average / Min / Max / Count

Fold rows down to a single number — a query or a `List<T>` you already hold. The one thing to know before you use them:…

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…