node/deps/npm/node_modules/sorted-object
isaacs bd547d6598 npm: upgrade to 1.4.6
* Documentation upgrades
* Fix glob bug which prevents proper README publishing
* node-gyp upgrade to 0.13
* Documentation updates
* Add --save-exact to save an exact dep (instead of a range)
* alias 't' to 'test'
2014-03-25 17:42:22 -07:00
..
lib npm: upgrade to 1.4.6 2014-03-25 17:42:22 -07:00
LICENSE.txt npm: upgrade to 1.4.6 2014-03-25 17:42:22 -07:00
README.md npm: upgrade to 1.4.6 2014-03-25 17:42:22 -07:00
package.json npm: upgrade to 1.4.6 2014-03-25 17:42:22 -07:00

README.md

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));