Sort an object array by one or more properties even nested properties. Besides, you can determine the direction even supply a comparison function in each property sorting.
Install
Install with npm:
$ npm install --save arr-sort
Install with yarn:
$ yarn add arr-sort
Usage
Sort an array by the given object property:
var arrSort = require('arr-sort');
arrSort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}],[{attr:'foo'}]);
//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]
Reverse order
arrSort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}],[{attr:'foo', asc: false}]);
//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]
Params
arrSort(array, comparisonArgs);
array: {Object Array} The object array to sortcomparisonArgs: {Object Array} One or more objects to sort by. The element structure is like this: {'attr':attr, 'asc':asc}attr: {String} the attribute of the objectasc: {Boolean|Function} point the direaction of sorting.true: sort by ascending direction (default)false: sort by descending directionfunction: sort by a comparable function
Note
- If
attris not found in object, this sorting round would be skip. - The value of
attrcan be a string or a number.- If string, we use
localeCompareto sort by. - If number, we just compare the amount of the number.
- If string, we use
- If there is not a return value of
function, this sorting round would be skip.
Examples
1. Sort by multiple properties
var arrSort = require('arr-sort');
var array = [
{ foo: 'bbb', num: 4, flag: 2 },
{ foo: 'aaa', num: 3, flag: 1 },
{ foo: 'ccc', num: -6, flag: 2 },
{ foo: 'ccc', num: 8, flag: 2 },
{ foo: 'bbb', num: 2, flag: 4 },
{ foo: 'aaa', num: -3, flag: 4 }
];
// sort by `flag`, then `foo`, then `num`
var result = arrSort(array,
[{
attr: 'flag',
asc: true
},
{
attr: 'foo',
asc: false
},
{
attr: 'num',
asc: true
}]
);
console.log(result);
// [ { foo: 'aaa', num: 3, flag: 1},
// { foo: 'ccc', num: -6, flag: 2},
// { foo: 'ccc', num: 8, flag: 2},
// { foo: 'bbb', num: 4, flag: 2},
// { foo: 'bbb', num: 2, flag: 4},
// { foo: 'aaa', num: -3, flag: 4} ]
2. Sort by nested properties
var arrSort = require('arr-sort');
var array = [
{ locals: { foo: 'bbb', num: 4 }, flag: 2},
{ locals: { foo: 'aaa', num: 3 }, flag: 1},
{ locals: { foo: 'ccc', num: -6 }, flag: 2},
{ locals: { foo: 'ccc', num: 8 }, flag: 2},
{ locals: { foo: 'bbb', num: 2 }, flag: 4},
{ locals: { foo: 'aaa', num: -3 }, flag: 4},
];
// sort by `flag`, then `locals.foo`, then `locals.num`
var result = arrSort(array,
[{
attr: 'flag',
asc: true
},
{
attr: 'locals.foo',
asc: false
},
{
attr: 'locals.num',
asc: true
}]
);
console.log(result);
// [ { locals: { foo: 'aaa', num: 3 }, flag: 1},
// { locals: { foo: 'ccc', num: -6 }, flag: 2},
// { locals: { foo: 'ccc', num: 8 }, flag: 2},
// { locals: { foo: 'bbb', num: 4 }, flag: 2},
// { locals: { foo: 'bbb', num: 2 }, flag: 4},
// { locals: { foo: 'aaa', num: -3 }, flag: 4} ]
3. Sort by custom function
If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the docsArray.sort() for more details.
var arrSort = require('arr-sort');
var array = [
{ locals: { foo: 'bbb', num: 4 }, flag: -2},
{ locals: { foo: 'aaa', num: 3 }, flag: 1},
{ locals: { foo: 'ccc', num: -6 }, flag: 2},
{ locals: { foo: 'ccc', num: 8 }, flag: 2},
{ locals: { foo: 'bbb', num: 2 }, flag: 4},
{ locals: { foo: 'aaa', num: -3 }, flag: 4},
];
// sort by `flag`, then `locals.foo`, then `locals.num`
var result = arrSort(array,
[{
attr: 'flag',
asc: function(a,b){return (Math.abs(a) - Math.abs(b))}
},
{
attr: 'locals.foo',
asc: false
},
{
attr: 'locals.num',
asc: true
}]
);
console.log(result);
// [ { locals: { foo: 'aaa', num: 3 }, flag: 1},
// { locals: { foo: 'ccc', num: -6 }, flag: 2},
// { locals: { foo: 'ccc', num: 8 }, flag: 2},
// { locals: { foo: 'bbb', num: 4 }, flag: -2},
// { locals: { foo: 'bbb', num: 2 }, flag: 4},
// { locals: { foo: 'aaa', num: -3 }, flag: 4} ]
About
Related projects
Running tests
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
Author
tywei90
License
Copyright © 2018, tywei90. Released under the MIT License.