sort-json-array
Sort an array of JSON objects by a property
Install
Install with npm:
$ npm install sort-json-array --save
Usage
Sort an array of JSON objects by a property:
var sortJsonArray = ; ;//=> [{name: 'a'}, {name: 'b'}, {name: 'c'}] ;//=> [{name: 'c'}, {name: 'b'}, {name: 'a'}] ;//=> [{name: 'a'}, {name: 'b'}, {name: 'c'}]
Params
;
array
: {Array} The array to be sorted.property
: {String}: The Property based on what the array should be sorted.order
: {String}: Pass 'des' if you want array to be sorted in descending order. Pass 'asc' if you want array to be sorted in ascending order. By default it will be sorted in ascending order.
Examples
var sortJsonArray = ; var user = name: 'c' location: "San Jose" name: 'a' location: "San Francisco" name: 'b' location: "New York" ; // sort by `name` in ascending order because order is not passedconsole; // sort by `location` in descending orderconsole; // sort by `name` in ascending orderconsole;
Running tests
Install dev dependencies and run test:
$ npm install -d && npm test
Author
Apurva Patel
License
Copyright © 2016, Apurva Patel. Released under the MIT license.