Type-safe comparator functions for Array.sort() operations with advanced
field access.
GaffComparator provides a collection of specialized comparator functions
designed to work seamlessly with Array.sort() and testing frameworks like
TestValidator.sort(). Each comparator supports both single values and arrays
of values, enabling complex multi-field sorting scenarios with lexicographic
ordering.
Key features:
Generic type safety for any object structure
Support for single values or arrays of values per field
Lexicographic comparison for multi-value scenarios
Locale-aware string comparison
Automatic type conversion for dates and numbers
The comparators follow the standard JavaScript sort contract:
Return < 0 if first element should come before second
Return > 0 if first element should come after second
Return 0 if elements are equal
Processing cost: Each comparison extracts both key lists and visits their
common prefix until the first difference; date keys additionally parse all
supplied strings once per comparison. No sort or repeated scan is performed
inside a comparison.
contracts/common.md#principled-implementation Each comparator maps both operands to arrays of comparable values and decides by the first position where they differ, then by array length, which is a lexicographic order that satisfies the sort contract for well-defined values; it is used to check server-side sorting from the client.
Evidence
contracts/common.md#clear-and-simple-design Three comparators share scalar-to-array wrapping. Strings compare each key by collation; dates and numbers use a shared strict mismatch search over their numeric keys.
Evidence
contracts/common.md#prohibited-implementation-shortcuts The ordering rule is generic in the getter, with no field, entity, or locale special case.
Evidence
contracts/common.md#meaningful-documentation The namespace prose lists the features and the sort contract with an example.
Type-safe comparator functions for Array.sort() operations with advanced field access.
GaffComparator provides a collection of specialized comparator functions designed to work seamlessly with Array.sort() and testing frameworks like TestValidator.sort(). Each comparator supports both single values and arrays of values, enabling complex multi-field sorting scenarios with lexicographic ordering.
Key features:
The comparators follow the standard JavaScript sort contract:
Processing cost: Each comparison extracts both key lists and visits their common prefix until the first difference; date keys additionally parse all supplied strings once per comparison. No sort or repeated scan is performed inside a comparison.
Author
Jeongho Nam - https://github.com/samchon
Example
Evidence
contracts/common.md#principled-implementation Each comparator maps both operands to arrays of comparable values and decides by the first position where they differ, then by array length, which is a lexicographic order that satisfies the sort contract for well-defined values; it is used to check server-side sorting from the client.
Evidence
contracts/common.md#clear-and-simple-design Three comparators share scalar-to-array wrapping. Strings compare each key by collation; dates and numbers use a shared strict mismatch search over their numeric keys.
Evidence
contracts/common.md#prohibited-implementation-shortcuts The ordering rule is generic in the getter, with no field, entity, or locale special case.
Evidence
contracts/common.md#meaningful-documentation The namespace prose lists the features and the sort contract with an example.