In addition, if you want to return the array in descending order, you can use the function reverse():
var obj = {x: 2, y: 6, z: 1, q: 4};
var keys = Object.keys(obj);
keys.sort(function(a, b) {
return obj[a] - obj[b]
}).reverse().forEach(function(k) {
console.log(obj[k]);
});
or just like that (thanks @muistooshort):
var obj = {x: 2, y: 6, z: 1, q: 4};
var keys = Object.keys(obj);
keys.sort(function(a, b) {
return obj[b] - obj[a]
}).forEach(function(k) {
console.log(obj[k]);
});
source
share