Last active
February 18, 2022 14:40
-
-
Save edwinwright/c5a2d507147e726809bb348d6eb7d47c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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