如何根据另一个数组的值对数组进行排序

How to sort an array based on values of another array?

本文关键字:数组 排序 何根 另一个      更新时间:2023-09-26

我有一个数组,它具有以下值

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','d3','d2','d0']对它们进行分组;并基于每个值的数目对每个组进行排序。所以输出应该是

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;
          }
        }        
    }
 }

使用第一个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);

EDIT:此方法将按照filterArray可以按任何顺序进行排序,并指定所需的顺序。在OP更新后,这可能不是要求。。。第二版:这个问题被修改得越来越多,这个解决方案将不起作用。

我的解决方案。此解决方案的唯一限制是必须对排序数组进行排序。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);
}

和一个工作小提琴

基于筛选数组是按字母顺序排列的,并且每个字符串都有一个格式为_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);