## Usage Sort an array by the given object property: ```js var arraySort = require('{%= name %}'); // or for ES6 imports: import arraySort from 'array-sort' var arr = [{foo: 'y'}, {foo: 'z'}, {foo: 'x'}]; arraySort(arr, 'foo'); //=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}] ``` **Reverse order** ```js var arr = [{foo: 'y'}, {foo: 'z'}, {foo: 'x'}]; arraySort(arr, 'foo', {reverse: true}); //=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}] ``` ## Params ```js arraySort(array, comparisonArgs); ``` - `array`: **{Array}** The array to sort - `comparisonArgs`: **{Function|String|Array}**: One or more functions or object paths to use for sorting. ## Examples **[Sort blog posts](examples/blog-posts.js)** ```js var arraySort = require('{%= name %}'); 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')); ``` **[Sort by multiple properties](examples/multiple-props.js)** ```js var arraySort = require('{%= name %}'); 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' } } ] ``` **[Custom function](examples/custom-function.js)** If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the [docs for `Array.sort()`][mozilla] for more details. ```js 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' } ] ``` **[Multiple custom functions](examples/custom-functions.js)** ```js 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' } ] ``` [mozilla]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort