Skip to content

Instantly share code, notes, and snippets.

@hogedigo
Created September 9, 2014 11:34
Show Gist options
  • Save hogedigo/b676a86b423ca7314250 to your computer and use it in GitHub Desktop.
Save hogedigo/b676a86b423ca7314250 to your computer and use it in GitHub Desktop.
sorting keys in JSON
var json = JSON.stringify(data, function(key, value) {
if (value instanceof Object === false || Object.getPrototypeOf(value) !== Object.prototype) {
return value;
}
var keys = Object.keys(value);
keys.sort();
var newValue = {};
keys.forEach(function(key) {
newValue[key] = value[key];
});
return newValue;
}, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment