Here is the fiddle.
I am trying to sort properties of the object by value.index
property:
const arr = {
field1: { data: "C", index: 3 },
field2: { data: "D", index: 4 },
field3: { data: "B", index: 2 },
field4: { data: "A", index: 1 },
};
const data = Object.entries(arr)
const sortedDataArray = data.sort((a, b) => a[1].index - b[1].index)
const result = Object.fromEntries(sortedDataArray)
console.log(result);
But I am getting back the same result as I passed it.
How to sort object properties by index which is value of the property?
Result should be field4, field3, field1, field2.