Order array by predefined rules

后端 未结 4 1547
猫巷女王i
猫巷女王i 2020-12-22 06:29

I have an array of currencies [\"GBP\", \"EUR\", \"NOK\", \"DKK\", \"SKE\", \"USD\", \"SEK\", \"BGN\"]. I would like to order it by moving predefined list if th

相关标签:
4条回答
  • 2020-12-22 06:42

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

    var tabPredef = ['EUR', 'USD', 'DKK', 'SKE', 'NOK', '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)
    
    0 讨论(0)
  • 2020-12-22 06:44

    You could use sorting with map and a hash table for the sort order. If the value is not in the hash table, the original order is taken.

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

    0 讨论(0)
  • 2020-12-22 06:49

    here is my solution. :-)

    //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' ]

    hope this is what u need :-)

    0 讨论(0)
  • 2020-12-22 06:57

    I guess this can also be achieved like this

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

    0 讨论(0)
提交回复
热议问题