sort-object-keys
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/sort-object-keys package

1.1.3 • Public • Published

Sort Object

Build Status

Returns a copy of an object with all keys sorted.

The second argument is optional and is used for ordering - to provide custom sorts. You can either pass an array containing ordered keys or a function to sort the keys (same signature as in Array.prototype.sort()).

const assert = require('assert');
const sortObject = require('sort-object-keys');
 
assert.equal(JSON.stringify({
  c: 1,
  b: 1,
  d: 1,
  a: 1,
}), JSON.stringify({
  a: 1,
  b: 1,
  c: 1,
  d: 1,
}));
 
assert.equal(JSON.stringify(sortObject({
  c: 1,
  b: 1,
  d: 1,
  a: 1,
}, ['b', 'a', 'd', 'c'])), JSON.stringify({
  b: 1,
  a: 1,
  d: 1,
  c: 1,
}));
 
function removeKeyAncCompareIndex(keyA, keyB){
  var a = parseInt(keyA.slice(4));
  var b = parseInt(keyB.slice(4));
  return a - b;
}
 
assert.equal(JSON.stringify(sortObject({
  "key-1": 1,
  "key-3": 1,
  "key-10": 1,
  "key-2": 1,
}, removeKeyAncCompareIndex)), JSON.stringify({
  "key-1": 1,
  "key-2": 1,
  "key-3": 1,
  "key-10": 1,
}));

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.3
    1,428,991
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.1.3
    1,428,991
  • 1.1.2
    8,580
  • 1.1.1
    0
  • 1.1.0
    0
  • 1.0.0
    1

Package Sidebar

Install

npm i sort-object-keys

Weekly Downloads

1,437,572

Version

1.1.3

License

MIT

Unpacked Size

2.69 kB

Total Files

3

Last publish

Collaborators

  • keithamus