Skip to content

Ordering

Ordering: object

Defined in: Core/Ordering.ts:17

by: <A, B>(extract) => (ordering) => 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

(value) => A

(ordering) => 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>

Ordering.thenBy to chain two orderings.

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 returning -1, 0, or 1.

Ordering.number for numeric ordering.

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

number: Ordering<number> = numberOrd

Numeric ordering returning -1, 0, or 1.

Ordering.date for chronological ordering.

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

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

Flips the direction of an ordering.

A

Ordering<A>

Ordering<A>

Ordering.thenBy to combine orderings with tiebreakers.

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

string: Ordering<string> = stringOrd

Alphabetical ordering for strings.

Ordering.number for numeric ordering.

Ordering.string("apple", "banana"); // -1

thenBy: <A>(other) => (ordering) => 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>

(ordering) => 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>

Ordering.byFields for ordering homogenous values with multiple comparators.

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