I am currently facing a challenge with sorting an array of objects by their values. In order to achieve this, I am using the following code:
function compare(a,b){return dict[a]-dict[b]}
Object.keys(dict).sort(compare)
This implementation works perfectly when all values are unique. However, when two objects share the same value, the sorting leaves them in their original order instead of arranging them alphabetically as I desire. Unfortunately, I have not been able to find a solution to address this issue.
The initial object {a:1, d:4, c:2, b:4, e:5, f:3}
should be sorted as follows:
{a:1, c:2, f:3, b:4, d:4, e:5 }
But the current output that I am getting is:
{a:1, c:2, f:3, d:4, b:4, e:5 }