The type of items in the stream
A task that sorts items
Comparator Functions:
Use comparator utilities from the @metreeca/core comparators module to create complex sorting criteria.
Default Behavior:
The default ascending comparator sorts values in natural order: numbers numerically, strings lexicographically, dates chronologically, and booleans with false before true. Null and undefined values are placed at the beginning.
Memory Usage:
Accumulates all stream items in memory before sorting. For large or infinite streams, this may cause memory issues or never complete.
import { ascending, descending, by, chain } from "@metreeca/core/comparators";
// Sort numbers (natural ascending order)
await items([3, 1, 2])(sort())(toArray()); // [1, 2, 3]
// Descending order
await items([3, 1, 2])(sort(descending))(toArray()); // [3, 2, 1]
// Sort by extracted key
await items([{age: 30}, {age: 20}])(sort(by(x => x.age)))(toArray());
// [{age: 20}, {age: 30}]
// Sort by key in descending order
await items([{age: 20}, {age: 30}])(sort(by(x => x.age, descending)))(toArray());
// [{age: 30}, {age: 20}]
// Custom comparator for locale-aware sorting
await items(["Émile", "Alice"])(sort((a, b) => a.localeCompare(b)))(toArray());
// Sort by name length, then alphabetically
await items(people)(sort(chain(
by(p => p.name.length),
by(p => p.name)
)))(toArray());
Creates a task sorting items in the stream.
All items are collected into memory before sorting, then yielded in sorted order.