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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment