I have a array like this:
arr[1] = 100
arr[10] = 20
arr[20] = 10
When I iterate the array, I got:
arr[1]:100 arr[10]:20 arr[20]:10
Currently, the array is sorted by the index. How can I sort it by the value but KEEP the original index.
What I want is:
arr[20]:10 arr[10]:20 arr[1]:100
I checked other posts but didn't find a solution to this specific issue. I am not sure javascript supports this. Can I get some help?
Thanks!
arr.length - 1. Even if you could reliably change the order of properties (indexes), with most iteration methods that becomes irrelevant. Use a different data structure that keeps order independent of keys, e.g.[[20, 10], ...].