javascript - How do I get the keys of the sorted values in an array -
if have following array: [5, 1, -7, 3, 6, 8, 0, -1, -3]
by sorting [-7, -3, -1, 0, 1, 3, 5, 6, 8]
that's fine, want keys array when sorted. this: [2, 8, 7, 6, 1, 3, 0, 4, 5]
i tried following using insertion sort, of course wrong.
var arr = [5, 1, -7, 3, 6, 8, 0, -1, -3]; keys = new array(arr.length); for(var j = 1; j < arr.length; j++) { key = arr[j]; var = j - 1; while(i >= 0 && arr[i] > key) { keys[i+1] = i; i--; } arr[i+1] = key; keys[i+1] = j; }
am on right track? can me out here :)
try kind of thing described on page
http://www.webdotdev.com/nvd/content/view/878/.
basically, make each item in array object 2 properties (the sort-key , index in array), sort them key.
Comments
Post a Comment