Created
February 19, 2021 03:48
-
-
Save larryyangsen/0e86b43efd7570938aba0a87624dcc16 to your computer and use it in GitHub Desktop.
Sort Json Keys by alphabet
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 ao = { | |
g: 3, | |
acd: 3, | |
a: 1, | |
b: 2, | |
ddd:6, | |
delete: 1, | |
d: 3, | |
ccc: 1, | |
}; | |
const sortedKeys = Object.keys(ao).sort((a, b) => a.localeCompare(b, 'en', { numeric: true })); | |
console.log('sortedKeys: ', sortedKeys); | |
const newAo = sortedKeys.reduce( | |
(o, k) => | |
Object.assign(o, { | |
[k]: ao[k], | |
}), | |
{} | |
); | |
console.log('newAo: ', newAo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment