Global collection of commonly used comparison functions.
These comparators are intended for use in sorted collections such as Queue.
Available comparators:
- ASCENDING: Sorts values from smallest to largest.
- DESCENDING: Sorts values from largest to smallest.
- INSERTION: Disables sorting; preserves insertion order.
- SINGULAR: Treats all values as equivalent — only one unique item is kept.
- BY_PROPERTY: Produces a comparator that sorts objects by one or more specified properties.
- REVERSE: Produces a comparator that inverts another comparator.
Members
(static) INSERTION
No sorting; preserves insertion order.
Methods
(static) ASCENDING()
Sorts primitive values in ascending order.
(static) BY_PROPERTY(names, comparatorsopt) → {function}
Creates a comparator function that sorts objects by one or more properties. Each property can have its own comparator; if fewer comparators than properties are provided, the last comparator is reused for the remaining properties.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
names |
string | Array.<string> | The property name or array of property names to compare. |
||
comparators |
function | Array.<function()> |
<optional> |
ORDER.ASCENDING | Comparator function(s) applied to property values.
Defaults to |
Returns:
A comparator function (a, b) => number that can be used in Array.sort or other sorting utilities.
- Type
- function
Example
// Sort by single property 'age' ascending
array.sort(ORDER.BY_PROPERTY('age'));
// Sort by multiple properties: first 'age' ascending, then 'name' descending
array.sort(ORDER.BY_PROPERTY(['age','name'], [ORDER.ASCENDING, ORDER.DESCENDING]));
(static) DESCENDING()
Sorts primitive values in descending order.
(static) REVERSE(comparator) → {function}
Returns a comparator that reverses the order of another comparator.
Example:
const cmp = ORDER.REVERSE(ORDER.ASCENDING);
items.sort(cmp); // same as DESCENDING
Parameters:
| Name | Type | Description |
|---|---|---|
comparator |
function | Any comparator |
Returns:
A comparator that negates the result of the given comparator.
- Type
- function
(static) SINGULAR()
Treats all values as equivalent.