[英]How to sort an array based on values of another array?
我有一个具有以下值的数组
Nata_sha_AD8_02_ABA
Jack_DD2_03_K
Alex_AD8_01_PO
Mary_CD3_03_DC
John_DD2_01_ER
Daniel_AD8_04_WS
我想基于以下数组['AD8','CD3','DD2','PD0']对它们进行分组; 并根据每个值的数量对每个组进行排序。 所以输出应该是
Alex_AD8_01_PO
Nata_sha_AD8_02_ABA
Daniel_AD8_04_WS
Mary_CD3_03_DC
John_DD2_01_ER
Jack_DD2_03_K
到目前为止,我编写了以下代码,但它无法正常工作,我被困在这里。
var temparr = [];
var order = 1000;
var pos = -1;
var temp = -1;
var filterArray= ['AD8','CD3','DD2','PD0'];
for (i =0; i< filterArray.length; i++) {
for (j =0; j < myarray.length; j++) {
if(filterArray[i].toUpperCase().search(myarray[j])>0){
temp = str.substring(myarray[j].indexOf(filterArray[i])+4, myarray[j].indexOf(filterArray[i]+6);
if(temp < order){
pos = j;
order = temp;
}
if(j == myarray.length-1){ //reached end of the loop
temparr.push(myarray[pos]);
order = 1000;
}
}
}
}
在过滤数组按字母顺序排列的基础上,并且每个字符串都有一个您实际想要排序的_XXN_NN_
格式的子字符串,仅仅基于提取该子字符串进行排序就足够了,而不需要引用filterArray
:
var names = ['Nata_sha_AD8_02_ABA', 'Jack_DD2_03_K', 'Alex_AD8_01_PO', 'Mary_CD3_03_DC', 'John_DD2_01_ER', 'Daniel_AD8_04_WS']; names.sort(function(a, b) { var re = /_((AD8|CD3|DD2|PD0)_\\d\\d)_/; a = a.match(re)[1]; b = b.match(re)[1]; return a.localeCompare(b); }); alert(names);
使用第一个sort
参数,您可以传递一个函数来运行以对数组进行排序。 此函数接收数组的2个值,并且应该比较它们并且如果第一个低于第二个则返回小于0,如果高于第二个则返回小于0,如果它们相同则返回0。 在我的命题中,我拆分了值的名称和“标记”部分,然后比较标记以正确排序它们。 使用filterArray上的indexOf
可以相应地比较标签的位置。
var array_to_sort = ['Natasha_AD8_02', 'Jack_DD2_03', 'Alex_AD8_01', 'Mary_CD3_03', 'John_DD2_01', 'Daniel_AD8_04' ]; var filterArray = ['AD8', 'CD3', 'DD2', 'PD0']; array_to_sort.sort(function(a, b) { a_token = a.substr(a.indexOf('_')+1); //Remove the name part as it is useless b_token = b.substr(b.indexOf('_')+1);//Remove the name part as it is useless if(a_token.substr(0,3) == b_token.substr(0,3)){//If the code is the same, order by the following numbers if(a_token > b_token){return 1;} if(a_token < b_token){return -1;} return 0; }else{ //Compare the position in the filterArray of each code. if(filterArray.indexOf(a_token.substr(0,3)) > filterArray.indexOf(b_token.substr(0,3))){return 1;} if(filterArray.indexOf(a_token.substr(0,3)) < filterArray.indexOf(b_token.substr(0,3))){return -1;} return 0; } }); document.write(array_to_sort);
编辑:此方法将以filterArray可以按任何顺序排序的方式排序,并指示所需的顺序。 在OP更新之后,这可能不是必需的...... EDIT2:正在修改的问题越来越多,这个解决方案将不起作用。
我的解决方案 此解决方案的唯一限制是您的排序数组必须已经排序。 XXn_nn
部分可以在字符串中的任何位置,但它假设nn
部分始终跟随XXn
部分(如DD3_17
)。
var result=new Array();
var p,x;
//loop the 'search' array
for(var si=0,sl=sort.length;si<sl;si++){
//create new tmp array
var tmp=new Array();
//loop the data array
for(var ai=0,al=arr.length;ai<al;ai++){
var el=arr[ai];
//test if element still exists
if(typeof el=='undefined' || el=='')continue;
//test if element has 'XXn_nn' part
if(arr[ai].indexOf(sort[si]) > -1){
//we don't now where the 'XXn_nn' part is, so we split on '_' and look for it
x=el.split('_');
p=x.indexOf(sort[si]);
//add element to tmp array on position nn
tmp[parseInt(x[p+1])]=el;
//remove element from ariginal array, making sure we don't check it again
arr.splice(ai,1);ai--;
}
}
//remove empty's from tmp array
tmp=tmp.filter(function(n){return n!=undefined});
//add to result array
result=result.concat(tmp);
}
还有一个工作小提琴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.