sort-keys-recursive
Sort the keys of an object recursively.
Install
npm install sort-keys-recursive --save
Usage
const sortKeysRecursive = require('sort-keys-recursive')
const object = {
c: 0,
a: {
c: ['c', 'a', 'b'],
a: 0,
b: 0
},
b: 0
}
const output = sortKeysRecursive(object)
console.log(output)
API
sortKeysRecursive(input, [options])
input
Required
Type: array
|object
The collection to be sorted.
options
compareFunction
Type: function
Compare function.
ignoreArrayAtKeys
Type: array
Don't sort the Array at the specified keys, if any.
ignoreObjectAtKeys
Type: array
Don't sort the Object at the specified keys, if any.
Examples
ignoreArrayAtKeys
and ignoreObjectAtKeys
const options = {
ignoreArrayAtKeys: [
'b'
],
ignoreObjectAtKeys: [
'a'
]
}
const input = {
a: {
a: 'a',
b: 'b',
c: 'c',
d: ['a', 'c', 'b']
},
b: ['a', 'c', 'b'],
d: ['a', 'c', 'b']
}
const output = sort(object, options)
console.log(output)
compareFunction
You can pass a custom sort function as compareFunction
. This function is passed to Javascript sort()
, that sorts in alphabetical order by default. The custom function should return zero, a negative or positive value:
const reverseAlphabeticalSort = function (a, b) {
return a < b
}
const options = {
compareFunction: reverseAlphabeticalSort
}
const object = {
a: {
a: 0,
c: ['c', 'a', 'b'],
b: 0
},
c: 0,
b: 0
}
const output = sort(object, options)
console.log(output)
License
MIT © Kiko Beats