Skip to content

Ordering

Ordering: object

Defined in: Core/Ordering.ts:17

by: <A, B>(f) => (ord) => Ordering<B>

Adapts an ordering for type A into an ordering for type B by extracting a field. Read as “ordering by this field”: pipe(Ordering.number, Ordering.by(p => p.price)).

A

B

(b) => A

(ord) => Ordering<B>

type Product = { name: string; price: number };
const byPrice = pipe(Ordering.number, Ordering.by((p: Product) => p.price));
pipe(products, Arr.sortWith(byPrice));

byFields: <A>(orderings) => Ordering<A>

Combines a list of orderings into a single composite comparator. Evaluates each ordering in sequence until a non-zero comparison result is found.

A

readonly Ordering<A>[]

Ordering<A>

const byName = pipe(Ordering.string, Ordering.by((u: User) => u.name));
const byAge  = pipe(Ordering.number, Ordering.by((u: User) => u.age));
const sortUsers = Ordering.byFields([byName, byAge]);

date: Ordering<Date> = dateOrd

Ordering for Date values by numeric time value.

pipe(dates, Arr.sortWith(Ordering.date)); // earliest first

number: Ordering<number> = numberOrd

Numeric ordering. Equivalent to (a, b) => a - b.

pipe([3, 1, 2], Arr.sortWith(Ordering.number)); // [1, 2, 3]

reverse: <A>(ord) => Ordering<A>

Flips the direction of an ordering.

A

Ordering<A>

Ordering<A>

pipe([3, 1, 2], Arr.sortWith(Ordering.reverse(Ordering.number))); // [3, 2, 1]

string: Ordering<string> = stringOrd

Alphabetical ordering for strings.

Ordering.string("apple", "banana"); // negative

thenBy: <A>(ord2) => (ord1) => Ordering<A>

Chains two orderings: the second is used only when the first returns 0. Data-last: the first ordering is the data being piped.

A

Ordering<A>

(ord1) => Ordering<A>

const byDeptThenSalary = pipe(byDept, Ordering.thenBy(bySalary));

tuple: <T>(…orderings) => Ordering<T>

Derives a lexicographical tuple ordering from positional Ordering comparators.

T extends readonly unknown[]

…{ [K in string | number | symbol]: Ordering<T[K]> }

Ordering<T>

const pairOrd = Ordering.tuple(Ordering.string, Ordering.number);
pairOrd(["a", 1], ["a", 2]); // negative