This module contains functions that can be used with Array.prototype.sort
.
npm install compare-property
var compare = require('compare-property');
[2, 1, 3].sort(compare.ascending) // [1, 2, 3]
Sort in ascending order.
[2, 1, 3].sort(compare.ascending) // [1, 2, 3]
Sort in descending order.
[2, 1, 3].sort(compare.descending) // [3, 2, 1]
Sort in case insensitive ascending order.
['a', 'B', 'c'].sort(compare.ascendingIgnoreCase) // ['a', 'B', 'c']
Sort in case insensitive descending order.
['a', 'B', 'c'].sort(compare.descendingIgnoreCase) // ['c', 'B', 'a']
Create function that sorts on a property. Specify sort order as 1 (ascending) or -1 (descending), default is ascending.
Arguments
var fn = compare.property('length', -1);
['ab', 'a', 'abc'].sort(fn) // ['abc', 'ab', 'a']
Create function that sorts on multiple properties. Specify sort order as 1 (ascending) or -1 (descending), default is ascending.
Arguments
var fn = compare.properties({ age: -1, name: 1 }, ['name']);
people.sort(fn);
// The people array will now be sorted on age from oldest to youngest.
// People with the same age will be sorted on name from A to Z ignoring case.