Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort Object By Value In Javascript
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}
@Syed714Ahsan
Copy link

Syed714Ahsan commented Aug 7, 2021

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',
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment