javscript object.entries.sort code example
Example 1: javascript sort object js
grossaryList = {
'bread': 1,
'apple': 6,
'milk': 1,
'orange': 3,
'broccoli': 2
}
return Object
.entries(grossaryList)
.sort((a,b) => b[1]-a[1])
Example 2: js object sort
var maxSpeed = {
car: 300,
bike: 60,
motorbike: 200,
airplane: 1000,
helicopter: 400,
rocket: 8 * 60 * 60
};
var sortable = [];
for (var vehicle in maxSpeed) {
sortable.push([vehicle, maxSpeed[vehicle]]);
}
sortable.sort(function(a, b) {
return a[1] - b[1];
});