Table of Contents
Sort array of objects based on property (supports nested properties)
Built with ❤︎ by Tiaan and contributors
Table of Contents
Install
$ npm install --save prop-sort# OR $ yarn add prop-sort
Usage
The original array is not mutated.
const propSort = const data = value: 1 priority: 'c' value: 2 priority: 'a' value: 9 value: 1 priority: 'z' value: 2 priority: 'a' console//[ { value: 9 },// { value: 2, priority: 'a' },// { value: 2, priority: 'a' },// { value: 1, priority: 'c' },// { value: 1, priority: 'z' } ]
Use dot notation
to sort by nested properties:
const propSort = const data = value: 1 foo: bar: 8 value: 2 foo: bar: 1 value: 9 foo: bar: 5 value: 1 foo: bar: 99 value: 2 foo: bar: 70 console// [ { value: 2, foo: { bar: 1 } },// { value: 9, foo: { bar: 5 } },// { value: 1, foo: { bar: 8 } },// { value: 2, foo: { bar: 70 } },// { value: 1, foo: { bar: 99 } } ]
Can also use own custom comparator function:
const propSort = data = ...
Contribute
Contributions are welcome. Please open up an issue or create PR if you would like to help out.
Note: If editing the README, please conform to the standard-readme specification.
License
Licensed under the MIT License.