Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

sort-deep-object-arrays

1.1.2 • Public • Published

sort-deep-object-arrays

NPM version Travis-CI

Sort objects and arrays of objects nested deeply within other objects or arrays

Requirements

Usage

Install as a local dependency for your project:

yarn add sort-deep-object-arrays

Run in command line

npx sort-deep-object-arrays /path/to/input/file.json

Require and use it in your code:

const sortDeepObjectArrays = require('sort-deep-object-arrays');
 
const myUnsortedObject = {
    b: 2,
    a: 1,
    d: [3, 5, 1],
    c: 3,
    e: [
        {
            f: 7,
            g: 8,
            h: 9
        },
        {
            g: 2,
            h: 3,
            f: 1
        },
        {
            g: 5,
            f: 4,
            h: 6
        }
    ]
};
 
const mySortedObject = sortDeepObjectArrays(myUnsortedObject);
 
console.log(JSON.stringify(mySortedObject, null, 2));

This should output:

{
  "a": 1,
  "b": 2,
  "c": 3,
  "d": [
    1,
    3,
    5
  ],
  "e": [
    {
      "f": 1,
      "g": 2,
      "h": 3
    },
    {
      "f": 4,
      "g": 5,
      "h": 6
    },
    {
      "f": 7,
      "g": 8,
      "h": 9
    }
  ]
}

Tests

Run tests with npm test. Written in tape. Automatically tested using Travis CI

Tests run against Node 0.12, 4, 6, 8, 10 and 12.

Install

npm i sort-deep-object-arrays

DownloadsWeekly Downloads

302

Version

1.1.2

License

MIT

Unpacked Size

9.27 kB

Total Files

7

Last publish

Collaborators

  • avatar