I have a sorting problem in javascript, I need to sort an array and sort the captions (saved in another array) in the same order (descending), how can I sort them the same way?
For the clarity of my post I will reduce it to a basic example:
var arr = Array(9, 5, 11, 2, 3);
var arrCaptions = Array("some text","another bit of text","three", "four?", "maybe five?");
Now I want to run a kind of sort mechanism that sorts the arrCaptions array the same way as the arr array, so as result you would get this:
var arrResult = Array(11, 9, 5, 3, 2);
var arrCaptionsResult = Array("three", "some text" ,"another bit of text", "maybe five?", "four?");
What I have tried so far doesn't work at all:
var numlist = Array(9, 5, 11, 2, 3);
var list = Array("four?","maybe five?","another bit of text","some text","three");
var resultnumlist = Array();
var resultlist = Array();
resultnumlist[0] = numlist[0];
resultlist[0] = list[0];
for (i = 0; i < list.length; i++) {
var i2 = list.length - 1;
while (numlist[i] < resultnumlist[i2]) {
i2--;
}
resultnumlist.splice(i2 - 1,0,numlist[i]);
resultlist.splice(i2 - 1,0,list[i]);
}