Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.
Install with npm:
$ npm install --save array-sort
Install with yarn:
$ yarn add array-sort
Sort an array by the given object property:
var arraySort = require('array-sort');
arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo');
//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]
Reverse order
arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true});
//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]
arraySort(array, comparisonArgs);
array
: {Array} The array to sortcomparisonArgs
: {Function|String|Array}: One or more functions or object paths to use for sorting.
var arraySort = require('array-sort');
var posts = [
{ path: 'c.md', locals: { date: '2014-01-09' } },
{ path: 'a.md', locals: { date: '2014-01-02' } },
{ path: 'b.md', locals: { date: '2013-05-06' } },
];
// sort by `locals.date`
console.log(arraySort(posts, 'locals.date'));
// sort by `path`
console.log(arraySort(posts, 'path'));
var arraySort = require('array-sort');
var posts = [
{ locals: { foo: 'bbb', date: '2013-05-06' }},
{ locals: { foo: 'aaa', date: '2012-01-02' }},
{ locals: { foo: 'ccc', date: '2014-01-02' }},
{ locals: { foo: 'ccc', date: '2015-01-02' }},
{ locals: { foo: 'bbb', date: '2014-06-01' }},
{ locals: { foo: 'aaa', date: '2014-02-02' }},
];
// sort by `locals.foo`, then `locals.date`
var result = arraySort(posts, ['locals.foo', 'locals.date']);
console.log(result);
// [ { locals: { foo: 'aaa', date: '2012-01-02' } },
// { locals: { foo: 'aaa', date: '2014-02-02' } },
// { locals: { foo: 'bbb', date: '2013-05-06' } },
// { locals: { foo: 'bbb', date: '2014-06-01' } },
// { locals: { foo: 'ccc', date: '2014-01-02' } },
// { locals: { foo: 'ccc', date: '2015-01-02' } } ]
If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the docs for Array.sort()
for more details.
var arr = [
{one: 'w', two: 'b'},
{one: 'z', two: 'a'},
{one: 'x', two: 'c'},
{one: 'y', two: 'd'},
];
function compare(prop) {
return function (a, b) {
return a[prop].localeCompare(b[prop]);
};
}
var result = arraySort(arr, function (a, b) {
return a.two.localeCompare(b.two);
});
console.log(result);
// [ { one: 'z', two: 'a' },
// { one: 'w', two: 'b' },
// { one: 'x', two: 'c' },
// { one: 'y', two: 'd' } ]
var arr = [
{foo: 'w', bar: 'y', baz: 'w'},
{foo: 'x', bar: 'y', baz: 'w'},
{foo: 'x', bar: 'y', baz: 'z'},
{foo: 'x', bar: 'x', baz: 'w'},
];
// reusable compare function
function compare(prop) {
return function (a, b) {
return a[prop].localeCompare(b[prop]);
};
}
// the `compare` functions can be a list or array
var result = arraySort(arr, compare('foo'), compare('bar'), compare('baz'));
console.log(result);
// [ { foo: 'w', bar: 'y', baz: 'w' },
// { foo: 'x', bar: 'x', baz: 'w' },
// { foo: 'x', bar: 'y', baz: 'w' },
// { foo: 'x', bar: 'y', baz: 'z' } ]
- get-value: Use property paths (
a.b.c
) to get a nested value from an object. | homepage - set-value: Create nested values and any intermediaries using dot notation (
'a.b.c'
) paths. | homepage - sort-asc: Sort array elements in ascending order. | homepage
- sort-desc: Sort array elements in descending order. | homepage
- sort-object: Sort the keys in an object. | homepage
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Commits | Contributor |
---|---|
10 | jonschlinkert |
4 | doowb |
1 | iamstolis |
1 | wkevina |
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on September 11, 2017.