Based on javascript-natural-sort by Jim Palmer, JavaScript Natural Multisort adds support for "naturally" sorting arrays of objects by a property and also for sorting an array in descending order.
Note: There is also an AngularJS orderBy comparator available: https://github.com/miyakoj/angularjs-natural-sort
var sortedArray = jsMultisort({array: array, property: "property", insensitive: insensitive, reverse: reverse});
- array: the array to be sorted
- property: the object property to sort on in string format, defaults to undefined
- insensitive: whether or not to ignore capitalization, defaults to false
- reverse: whether or not to sort in descending order, defaults to false
All configuration options are optional except for array. For example, if you want to sort a simple array in reverse order, you would use jsMultisort({array: array, reverse: true}).
Note: You can also sort an array of complex objects, but only on a property with a scalar value (not an array or an object) in the first dimension.
>>> var sortedArray = jsMultisort({array: ['a', 'B']});
['B', 'a']
>>> var sortedArray = jsMultisort({array: ['a', 'B'], insensitive: true});
['a', 'B']
>>> var sortedArray = jsMultisort({array: ['10', 9, 2, '1', '4']});
['1', 2, '4', 9, '10']
>>> var sortedArray = jsMultisort({array: ['10.0401', 10.022, 10.042, '10.021999']});
['10.021999', 10.022, '10.0401', 10.042]
>>> var sortedArray = jsMultisort({array: ['10.04f', '10.039F', '10.038d', '10.037D']});
['10.037D', '10.038d', '10.039F', '10.04f']
>>> var sortedArray = jsMultisort({array: ['1.528535047e5', '1.528535047e7', '1.528535047e3']});
['1.528535047e3', '1.528535047e5', '1.528535047e7']
>>> var sortedArray = jsMultisort({array: ['192.168.0.100', '192.168.0.1', '192.168.1.1']});
['192.168.0.1', '192.168.0.100', '192.168.1.1']
>>> var sortedArray = jsMultisort({array: ['car.mov', '01alpha.sgi', '001alpha.sgi', 'my.string_41299.tif']});
['001alpha.sgi', '01alpha.sgi', 'car.mov', 'my.string_41299.tif']
>>> var sortedArray = jsMultisort({array: ['10/12/2008', '10/11/2008', '10/11/2007', '10/12/2007']});
['10/11/2007', '10/12/2007', '10/11/2008', '10/12/2008']
>>> var sortedArray = jsMultisort({array: ['$10002.00', '$10001.02', '$10001.01']});
['$10001.01', '$10001.02', '$10002.00']
>>> var sortedArray = jsMultisort({array: ['1 Title - The Big Lebowski', '1 Title - Gattaca', '1 Title - Last Picture Show']});
['1 Title - Gattaca', '1 Title - Last Picture Show', '1 Title - The Big Lebowski']
>>> var array = [
{name: "p1", number: 1},
{name: "p5", number: 5},
{name: "p10", number: 10},
{name: "p25", number: 25},
{name: "p2", number: 2}
];
>>> var sortedArray = jsMultisort({array: array, property: "name"});
[
{name: "p1", number: 1},
{name: "p2", number: 2},
{name: "p5", number: 5},
{name: "p10", number: 10},
{name: "p25", number: 25}
]
>>> var array = [
{letter: 'a', animals: [{name: 'antelope', number: 1}, {name: 'alligator', number: 2}]},
{letter: 'e', animals: [{name: 'elephant', number: 3}, {name: 'elk', number: 4}]},
{letter: 'c', animals: [{name: 'cat', number: 5}, {name: 'canary', number: 6}]},
{letter: 'b', animals: [{name: 'bear', number: 7}, {name: 'buffalo', number: 8}]},
{letter: 'd', animals: [{name: 'dog', number: 9}, {name: 'dragon', number: 10}]}
];
>>> var sortedArray = jsMultisort({array: array, property: "letter"});
[
{letter: 'a', animals: [{name: 'antelope', number: 1}, {name: 'alligator', number: 2}]},
{letter: 'b', animals: [{name: 'bear', number: 7}, {name: 'buffalo', number: 8}]},
{letter: 'c', animals: [{name: 'cat', number: 5}, {name: 'canary', number: 6}]},
{letter: 'd', animals: [{name: 'dog', number: 9}, {name: 'dragon', number: 10}]},
{letter: 'e', animals: [{name: 'elephant', number: 3}, {name: 'elk', number: 4}]}
]