How to sort objects by value
javascript
Solution
let obj = {a: 24, b: 12, c:21, d:15};
// Get an array of the keys:
let keys = Object.keys(obj);
// Then sort by using the keys to lookup the values in the original object:
keys.sort((a, b) => obj[a] - obj[b]);
console.log(keys);
Note that the above could be done in one line if desired with `Object.keys(obj).sort(...)`. The simple `.sort()` comparator function shown will only work for numeric values. Swap `a` and `b` to sort in the opposite direction.
Problem
Say you have the following object in JS: ``` let obj = {a: 24, b: 12, c:21; d:15}; ``` How can 'obj' be transformed into an array of the keys of the object, sorted by the values?