buildComparator
function buildComparator<Row>(input): object;Defined in: filters/memory.ts:725
Build an Array.prototype.sort comparator from a Sort, allow-listed to paths (public
field name → MemoryAccessor). Unknown fields are dropped; when nothing is left,
defaultSort is used instead (and with no keys at all every pair compares equal). Keys
apply in order; ties keep the input order, since Array.prototype.sort is stable.
Values compare like the filter predicate: numbers (and bigints) numerically, strings by
code point (SQLite’s BINARY / Postgres’s "C" collation, not a locale), instants (Date,
or anything with epochMilliseconds) by time, false before true. NULLs follow
dialect (Postgres: last ascending, first descending; SQLite: the opposite), or nulls
("first"/"last", like NULLS FIRST/NULLS LAST) whatever the direction.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
Row |
any |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
input |
{ defaultSort?: Sort; dialect?: MemoryDialect; nulls?: NullsOrder; paths: Record<string, MemoryAccessor<Row>>; sort: Sort; } |
input.defaultSort? |
Sort |
input.dialect? |
MemoryDialect |
input.nulls? |
NullsOrder |
input.paths |
Record<string, MemoryAccessor<Row>> |
input.sort |
Sort |
Returns
Section titled “Returns”object
| Name | Type | Defined in |
|---|---|---|
compare |
RowComparator<Row> |
filters/memory.ts:731 |
Example
Section titled “Example”import { buildComparator } from "@xtandard/lib/filters/memory";
const rows = [ { id: 1, name: "b", amount: 5 }, { id: 2, name: "a", amount: null }, { id: 3, name: "c", amount: 5 },];const { compare } = buildComparator({ sort: [{ field: "amount", dir: "desc" }, { field: "password", dir: "asc" }], // password: not sortable paths: { name: "name", amount: "amount" }, defaultSort: [{ field: "name", dir: "asc" }],});[...rows].sort(compare).map((r) => r.id); // → [2, 1, 3] (Postgres: NULLs first descending; ties keep input order)