Created
May 18, 2022 02:06
-
-
Save armenic/18c5b8e87673efc11f4b05a22a05dcb2 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