Created
November 28, 2023 12:31
-
-
Save renatocfrancisco/390965a0efd7a846db3a469d98492f8f to your computer and use it in GitHub Desktop.
sorted obj example
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 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