Skip to content

Instantly share code, notes, and snippets.

@renatocfrancisco
Created November 28, 2023 12:31
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 renatocfrancisco/390965a0efd7a846db3a469d98492f8f to your computer and use it in GitHub Desktop.
Save renatocfrancisco/390965a0efd7a846db3a469d98492f8f to your computer and use it in GitHub Desktop.
sorted obj example
const obj = {
908: '1083',
326: '1083',
1048: '77',
903: '77',
935: null,
149: null,
336: '626',
905: '1071',
955: '33',
353: '33',
70: '1064',
925: '1064',
901: '121',
961: '329',
739: '752',
320: '1126'
};
// Extract keys and sort them
const sortedKeys = Object.keys(obj).sort((a, b) => a - b);
// Create a new object with sorted keys
const sortedObject = {};
sortedKeys.forEach(key => {
sortedObject[key] = obj[key];
});
console.log(sortedObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment