Created
December 1, 2023 19:12
-
-
Save ChathuraGH/f3572e6a98b5f2f6dcb72721b1af9819 to your computer and use it in GitHub Desktop.
sortObjectByValues to dic
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 sortObjectByValues = (dict: { [key: string]: number }, direction: 'asc'| 'desc' = 'asc') => { | |
return Object.fromEntries(Object.entries(dict).sort((a, b) => { | |
if (direction === 'asc') { | |
return a[1] - b[1] | |
} | |
return b[1] - a[1] | |
})) | |
} | |
//source | |
// https://stackoverflow.com/questions/25500316/sort-a-dictionary-by-value-in-javascript | |
// https://stackoverflow.com/a/74380445/13861187 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment