Provides ready-made comparators, factories deriving them from keys or locales, and combinators that reverse, extend
or chain them. Factories and combinators return a Comparator, so results compose freely and plug into
Array.sort().
users.sort(by(user=>user.name, locale())); // Sort by name with the runtime locale
Handling Nullish Values
Place null and undefined keys first or last. Array.prototype.sort moves undefined array elements to the end
without ever consulting the comparator, so the placement of undefined values holds for keys extracted with by:
Composable comparison functions.
Provides ready-made comparators, factories deriving them from keys or locales, and combinators that reverse, extend or chain them. Factories and combinators return a Comparator, so results compose freely and plug into Array.sort().
Basic Sorting
Sort arrays using ascending or descending order:
Sorting by Property
Extract and compare specific properties from objects:
Sorting by Locale
Order strings according to the collation rules of a locale:
Handling Nullish Values
Place
nullandundefinedkeys first or last.Array.prototype.sortmoves undefined array elements to the end without ever consulting the comparator, so the placement of undefined values holds for keys extracted withby:Reversing Order
Invert an existing comparator:
Combining Multiple Criteria
Sort by multiple properties with priority order: