JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 24668
  • Score
    100M100P100Q147149F
  • License MIT

Sort the keys of an object recursively

Package Exports

  • sort-keys-recursive

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sort-keys-recursive) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sort-keys-recursive

Last version Build Status Coverage Status Dependency status Dev Dependencies Status NPM Status Donate

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)

// {
//   a: {
//     a: 0,
//     b: 0,
//     c: ['a', 'b', 'c']
//   },
//   b: 0,
//   c: 0
// };

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: [ // Don't sort the Array at the specified keys, if any.
    'b'
  ],
  ignoreObjectAtKeys: [ // Don't sort the Object at the specified keys, if any.
    'a'
  ]
}

const input = {
  a: { // This Object will not be sorted.
    a: 'a',
    b: 'b',
    c: 'c',
    d: ['a', 'c', 'b']
  },
  b: ['a', 'c', 'b'], // This Array will not be sorted.
  d: ['a', 'c', 'b']
}

const output = sort(object, options)

console.log(output)

// {
//   a: {
//     a: 'a',
//     b: 'b',
//     c: 'c',
//     d: ['a', 'c', 'b']
//   },
//   b: ['a', 'c', 'b'],
//   d: ['a', 'b', 'c']
// }

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:

var reverseAlphabeticalSort = function (a, b) {
  return a < b
}

var options = {
  compareFunction: reverseAlphabeticalSort
}

var object = {
  a: {
    a: 0,
    c: ['c', 'a', 'b'],
    b: 0
  },
  c: 0,
  b: 0
}

var output = sort(object, options)

console.log(output)

// {
//   c: 0,
//   b: 0,
//   a: {
//     c: ['c', 'b', 'a'],
//     b: 0,
//     a: 0
//   }
// }

License

MIT © Kiko Beats