按预定义规则排序数组

时间:2016-06-07 06:07:22

标签: javascript arrays sorting ecmascript-6

我有一系列货币["GBP", "EUR", "NOK", "DKK", "SKE", "USD", "SEK", "BGN"]。如果货币存在于数组的开头,我想通过移动预定义列表来订购它。预定义列表为['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP']。 所以在这种情况下它应该返回['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP', 'SEK', BGN']

但是如果未过滤的数组不包含预定义列表中的所有值,它也应该正确排序。例如:["GBP", "EUR", "NOK", "LTU", "ZGN"]应该看起来像['EUR', 'NOK', 'GBP', 'LTU', 'ZGN'

我试图使用此功能对其进行排序:

list.sort(c => ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP'].indexOf(c))

但是它将所有预定义的货币放在列表的末尾,而不是放在from。也许还有更好的方法吗?

4 个答案:

答案 0 :(得分:2)

您可以使用sorting with map和哈希表作为排序顺序。如果该值不在哈希表中,则采用原始顺序。



var order = ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP'],
    orderObj = Object.create(null),
    data = ["GBP", "EUR", "NOK", "DKK", "SKE", "USD", "SEK", "BGN"];

// generate hash table
order.forEach((a, i) => orderObj[a] = i + 1);

// temporary array holds objects with position and sort-value
var mapped = data.map((el, i) => { return { index: i, value: orderObj[el] || Infinity }; });

// sorting the mapped array containing the reduced values
mapped.sort((a, b) => a.value - b.value || a.index - b.index);

// assigning the resulting order
var data = mapped.map(el => data[el.index]);

console.log(data);




答案 1 :(得分:2)

我想这也可以像这样实现

Array.prototype.intersect = function(a) {
  return this.filter(e => a.includes(e));
};
Array.prototype.excludes = function(a) {
  return this.filter(e => !a.includes(e));
};
var getCur = (p,c) => p.intersect(c).concat(c.excludes(p)),
      cur1 = ["GBP", "EUR", "NOK", "DKK", "SKE", "USD", "SEK", "BGN"],
      cur2 = ["GBP", "EUR", "NOK", "LTU", "ZGN"],
       pdl = ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP', 'SEK', 'BGN'];
console.log(getCur(pdl,cur1));
console.log(getCur(pdl,cur2));

答案 2 :(得分:0)

这是我的解决方案。 : - )

//custom index of
Array.prototype.customIndexOf = function(a){
  return this.indexOf(a) === -1 ? Infinity : this.indexOf(a);
}

let orderArr = ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP'];


/*test case 1*/
let urList = ["GBP", "EUR", "NOK", "DKK", "SKE", "USD", "SEK", "BGN"];
urList.sort((a, b) => { return orderArr.customIndexOf(a) - orderArr.customIndexOf(b); });
console.log(urList); //[ 'EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP', 'SEK', 'BGN' ]

/*test case 2*/
let newList = ["GBP", "EUR", "NOK", "LTU", "ZGN"];

newList.sort((a, b) => { return orderArr.customIndexOf(a) - orderArr.customIndexOf(b); });
console.log(newList); //[ 'EUR', 'NOK', 'GBP', 'LTU', 'ZGN' ]

希望这是你需要的:-)

答案 3 :(得分:0)

var tabCurrency = [' GBP',' EUR',' NOK',' DKK',' SKE&#39 ;,' USD',' SEK',' BGN'];

var tabPredef = [' EUR',' USD',' DKK',' SKE',' NOK&#39 ;,' GBP'];

var newTabGood = [];

    tabPredef.forEach(function (itemPredef, indexPref) {

         var indexTemp;

         tabCurrency.forEach(function (itemCurrency, indexCurrency) {
               if(itemPredef == itemCurrency)
               {
                  newTabGood.push(itemPredef);
                  indexTemp = indexCurrency;
               }
         })

         tabCurrency.splice(indexTemp, 1)
   }) 


 var resultat = newTabGood.concat(tabCurrency);

 console.log(resultat)