Skip to content

Instantly share code, notes, and snippets.

@larryyangsen
Created February 19, 2021 03:48
Show Gist options
  • Save larryyangsen/0e86b43efd7570938aba0a87624dcc16 to your computer and use it in GitHub Desktop.
Save larryyangsen/0e86b43efd7570938aba0a87624dcc16 to your computer and use it in GitHub Desktop.
Sort Json Keys by alphabet
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