I have the following code:
//data_r is an array with values
var i = 0;
var sort_order = new Array();
data_r.sort(function (a,b) {
var res = a[0] - b[0];
sort_order[i] = res;
i++;
return res;
});
In the end, the sort_order array contains the actions performed when we sorted items. If I want to sort a second array exactly the same way as the first then I can do the following:
//data_x is an array with values
var i = 0;
data_x.sort(function (a,b) {
i++;
return sort_order[i-1];
});
Now the data_x array is sorted exactly the same way as the data_r array.
The question is, how can I undo sort on the data_r array?
The following code is incorrect:
var unsort = new Array();
for(var i = 0; i < data_r.length; i++)
unsort[i] = sort_order[i]*(-1);//-1 so we perfom the oposite action
Why, please?
EDIT:
I can't make a copy of the array.
I have array #1. I sort it.
Then I receive array #2 but the array is sorted based on array #1.
I need to reverse the sorting on array #2.
EDIT 2:
array #1 = {9, 5, 3, 0, 2}
I sort the array #1:
array #1 = {0, 2, 3, 5, 9}
NOW i receive array #2 sorted based on array #1:
array #2 = {"home", "car", "train", "pc", "mouse"}
I need to make array #2 like this:
array #2 = {"mouse, "pc", "train", "home", "car"}
solved: http://jsfiddle.net/fQm3a/
sortOrder[i] = res;
? There's no "j". – Pointy Jul 11 at 16:12