Skip to content

Instantly share code, notes, and snippets.

@edwinwright
Last active February 18, 2022 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwinwright/c5a2d507147e726809bb348d6eb7d47c to your computer and use it in GitHub Desktop.
Save edwinwright/c5a2d507147e726809bb348d6eb7d47c to your computer and use it in GitHub Desktop.
const unordered = {
b: "foo",
c: "bar",
a: "baz",
};
console.log(JSON.stringify(unordered));
// → '{"b":"foo","c":"bar","a":"baz"}'
const ordered = Object.keys(unordered)
.sort()
.reduce((obj, key) => {
obj[key] = unordered[key];
return obj;
}, {});
console.log(JSON.stringify(ordered));
// → '{"a":"baz","b":"foo","c":"bar"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment