【问题标题】:Sort array with numbers用数字对数组进行排序
【发布时间】:2018-06-29 22:00:30
【问题描述】:

这是我拥有的数组:

var myArray = [
  '<@424507945156784498> - 152,800$',
  '<@381223410610501732> - 100$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
];

这就是我想要的:

var newArray = [
  '<@424507945156784498> - 152,800$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
  '<@381223410610501732> - 100$',      
];

我尝试使用这种方法 - Javascript : natural sort of alphanumerical strings 但它按 id 排序 () 我如何按货币价值排序?

【问题讨论】:

  • 发布您尝试的代码。
  • 不是按 id 排序的。它是按整个字符串排序的,因为数组中的每个条目都是一个值......您附加的 SO 链接会去除非数字字符(基本上)并对数组进行排序。

标签: javascript node.js sorting discord.js


【解决方案1】:

拆分和替换 - 排序将对现有数组进行排序 - 如果您不需要对其进行变异,则需要将其复制到另一个数组:

var myArray = [
  '<@424507945156784498> - 152,800$',
  '<@381223410610501732> - 100$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
];
function toNum(str) {
  return +str.split(" - ")[1]  // get the amount
    .replace(/[^\d]/g,"");     // remove all non-numeric
}
myArray.sort(function(a,b) {
  return toNum(b)-toNum(a);    // numeric sort in situ
});
console.log(myArray)

【讨论】:

    【解决方案2】:

    只需提取价格值并按其排序:

    var myArray = [
      '<@424507945156784498> - 152,800$',
      '<@381223410610501732> - 100$',
      '<@224451506344606852> - 74,424$',
      '<@101441124537903160> - 65,100$'
    ];
    
    var result = myArray.sort((a,b) => {
    
      var [priceA, priceB] = [a,b].map(i => parseInt(i.split('-')[1].trim().replace(/\D/g,'')))
    
      return priceB - priceA;
    
    });
    
    console.log(result);
    

    【讨论】:

      【解决方案3】:

      使用map-sort-map 成语:

      1. 将要排序的新数据映射到现有元素上
      2. 按添加的数据排序
      3. 映射以删除排序依据的元素

      var myArray = [
        '<@424507945156784498> - 152,800$',
        '<@381223410610501732> - 100$',
        '<@224451506344606852> - 74,424$',
        '<@101441124537903160> - 65,100$'
      ];
          
      console.log(
        myArray.map(e => [e, e.split(' - ')[1].replace(/[^0-9]/g,'')])
               .sort((a, b) => b[1] - a[1])
               .map(e => e[0])
      );

      【讨论】:

      • @mplungjan 我想 OP 可以弄清楚如何分配给一个新变量。
      • 我想当他删除console.log并且没有变化时OP会感到困惑
      【解决方案4】:

      试试下面的表达式:

      myArray.sort((x,y)=>x.replace(/,/g,"").match(/(\d+)\$/)[1]*1 < y.replace(/,/g,"").match(/(\d+)\$/)[1]*1)
      

      解释:

      x.replace(/,/g,"").match(/(\d+)\$/)[1]*1
      

      此表达式删除逗号,然后匹配后跟$ 的数字。这是针对 sort 方法中使用的 x 和 y 完成的。

      var myArray = [
        '<@424507945156784498> - 152,800$',
        '<@381223410610501732> - 100$',
        '<@224451506344606852> - 74,424$',
        '<@101441124537903160> - 65,100$'
      ];
      
      console.log(myArray.sort((x,y)=>x.replace(/,/g,"").match(/(\d+)\$/)[1]*1 < y.replace(/,/g,"").match(/(\d+)\$/)[1]*1))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多