sorted-object
Returns a copy of an object with its keys sorted
Last updated 8 years ago by domenic .
(WTFPL OR MIT) · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install sorted-object 
SYNC missed versions from official npm registry.

Get a Version of an Object with Sorted Keys

Although objects in JavaScript are theoretically unsorted, in practice most engines use insertion order—at least, ignoring numeric keys. This manifests itself most prominently when dealing with an object's JSON serialization.

So, for example, you might be trying to serialize some object to a JSON file. But every time you write it, it ends up being output in a different order, depending on how you created it in the first place! This makes for some ugly diffs.

sorted-object gives you the answer. Just use this package to create a version of your object with its keys sorted before serializing, and you'll get a consistent order every time.

var sortedObject = require("sorted-object");

var objectToSerialize = generateStuffNondeterministically();

// Before:
fs.writeFileSync("dest.json", JSON.stringify(objectToSerialize));

// After:
var sortedVersion = sortedObject(objectToSerialize);
fs.writeFileSync("dest.json", JSON.stringify(sortedVersion));

Current Tags

  • 2.0.1                                ...           latest (8 years ago)

3 Versions

  • 2.0.1                                ...           8 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.0                                ...           11 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 2
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |