Created
October 13, 2018 06:58
-
-
Save nnnkit/acc7a271ebd50d132b62a46e83e0f6d6 to your computer and use it in GitHub Desktop.
Sort Object By Value In Javascript
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
function sortObjByValue(list){ | |
var sortedObj = {} | |
Object.keys(list) | |
.map(key => [key, list[key]]) | |
.sort((a,b) => a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : 0) | |
.forEach(data => sortedObj[data[0]] = data[1]); | |
return sortedObj; | |
} | |
// Example | |
var list = { | |
"you": 100, | |
"me": 75, | |
"foo": 116, | |
"bar": 15 | |
}; | |
sortObjByValue(list); | |
// {bar: 15, me: 75, you: 100, foo: 116} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to sort this by values(date) in java script
``var sortableData = {
'1': '2021-07-04 10:49',
'2': '2021-07-06 11:49',
'3': '2021-07-01 9:15',
'4': '2021-07-05 22:00',
'5': '2021-07-02 23:00',
'6': '2021-07-15 10:00',
'7': '2021-07-10 14:00',
'8': '2021-07-03 16:00',
}