I know objects are by default sorted out in ascending order or the way we will insert but I need the best way to sort the object in descending order. Ex:
Input x = {
a: {},
b: {},
c: {},
d: {}
}
Output: x = {
d: {},
c: {},
b: {},
a: {}
}
I tried the below solution, It works but is it a good practice or can I get anything better on this?
x= Object.keys(x).sort().reverse().reduce((obj, key) => {
obj[key] = x[key]
return obj
}, {})
Object.keys(x).sort((a, b) => b.localeCompare(a)).reduce()