-
-
Save alperg/19f666562059bd18b80a547dd53728df to your computer and use it in GitHub Desktop.
JavaScript Sort Object by Values Into Array of Objects
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
import assert from "assert"; | |
function main() { | |
let obj = { | |
big: 4, | |
biggest: 6, | |
bigger: 5, | |
small: 1, | |
}; | |
let sortedObjects = Object.keys(obj) | |
.sort((a, b) => obj[b] - obj[a]) | |
.map((key) => ({ [key]: obj[key] })); | |
return sortedObjects; | |
} | |
assert.deepEqual(main(), [ | |
{ biggest: 6 }, | |
{ bigger: 5 }, | |
{ big: 4 }, | |
{ small: 1 }, | |
]); | |
console.log("Success!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment