DEV Community

mhsohag11
mhsohag11

Posted on

Answer: Sorting object property by values

Sorting object property by values

const obj = { you: 100, me: 75, foo: 116, bar: 15 };
const keysSorted = Object.keys(obj).sort((a, b) => obj[a] - obj[b]);
const result = {};
keysSorted.forEach(key => { result[key] = obj[key]; });
document.write('Result: ' + JSON.stringify(result));

The desired output:

Top comments (0)