I have an object whose values I want to sort by descending order. Exemple:
//This
{
c: 34,
a: 30,
b: 21
}
// Instead of
{
a: 30,
b: 21,
c: 34
}
I can sort it this way:
function sortObject (obj){
return Object.entries(obj).sort((a, b) => (a[1] > b[1] ? 1 : -1))
}
Then, I need to transform this sorted array back into an object. No matter the method, the object goes back to auto-sorting its key by alphabetical order. How to keep my new order?
Here is the method I've used:
sortedObject.reduce((acc, [key, value]) => Object.assign(acc, { [key]: value }), {});
Thanks!