Skip to content

Instantly share code, notes, and snippets.

@ChathuraGH
Created December 1, 2023 19:12
Show Gist options
  • Save ChathuraGH/f3572e6a98b5f2f6dcb72721b1af9819 to your computer and use it in GitHub Desktop.
Save ChathuraGH/f3572e6a98b5f2f6dcb72721b1af9819 to your computer and use it in GitHub Desktop.
sortObjectByValues to dic
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