我在 javascript 中有一个排序问题,我需要对数组进行排序并以相同的顺序(降序)对标题(保存在另一个数组中)进行排序,我怎样才能以相同的方式对它们进行排序?
为了我的帖子清晰起见,我将其简化为一个基本示例:
var arr = Array(9, 5, 11, 2, 3);
var arrCaptions = Array("some text","another bit of text","three", "four?", "maybe five?");
现在我想运行一种排序机制,以与arr数组相同的方式对arrCaptions数组进行排序,因此您将得到以下结果:
var arrResult = Array(11, 9, 5, 3, 2);
var arrCaptionsResult = Array("three", "some text" ,"another bit of text", "maybe five?", "four?");
到目前为止我所尝试的根本不起作用:
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]);
}