javascript - sort array keys by value -


I am using JavaScript, and I have an array that has multiple values, which can be non-unique I want to take this array and in the ascending order of value I want to generate a new array of keys or order list. For example, if I have [2, 2, 4, 5, 1, 6], then I want to generate [5, 4, 0, 1, 2, 3].

I was thinking that at the time of recruiting on the original list and inserting each value into the new list, each place is merged, while comparing the existing values ​​of the new list while examining the appropriate placements. It seems useless, though, as I (possibly) have to check every value of the new list for every inclusion.

Does anyone have a simple method for this?

I think you mean [4, 0, 1, 2, 3, 5] . function GetSortedKeys (value) {var array_with_keys = []; (Var i = 0; i & lt; values.length; i ++) {array_with_keys.push ({key: i, value: value [i]}); } Array_with_keys.sort (function (a, b) {if (a.value & lt; b.value) {return -1;} if (a.value & gt; b.value) {return 1;} return 0; }); Var key = []; (Var i = 0; i & lt; array_with_keys.length; i ++) for {keys.push (array_with_keys [i] .key); } Return key; } Var array = [2, 2, 4, 5, 1, 6]; Alert (GetSortedKeys);

This is the easiest way, I can maliciously come to javascript.

Comments