sort object dictionary javascript descending order code example
Example 1: sort object dictionary javscript
var dict = {
"x": 1,
"y": 6,
"z": 9,
"a": 5,
"b": 7,
"c": 11,
"d": 17,
"t": 3
};
var items = Object.keys(dict).map(function(key) {
return [key, dict[key]];
});
items.sort(function(first, second) {
return second[1] - first[1];
});
console.log(items.slice(0, 5));
Example 2: sort array elements in descending order based on object key
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));