【问题标题】:I need help sorting specific numbers inside an array of strings to descend我需要帮助对字符串数组中的特定数字进行排序以下降
【发布时间】:2019-07-03 08:33:42
【问题描述】:

我有一个字符串数组,我需要根据每个字符串中的前 4 个数字对这些字符串进行排序。但是我如何针对这些数字进行排序呢?代码运行良好,我只需要对其进行排序。

我试过 .substring 但我认为它不是正确的方法。

var volunteers = [ '9.05', '16.29', '26.67', '0.00', '2.25' ]

getResults: function(volunteers) {
  this.results = [];
  for (var i = 0; i < volunteers.length; i++) {
    var dayNames = this.data[i][3] || " ";
    var result = (volunteers[i] + " additional volunteers are needed on day " + i +" " +dayNames);
    this.results.push(result);        
  }


console.log(this.results)
  return this.results;

}
//Expected
[ '26.67 additional volunteers are needed on day 2 Tuesday',
'16.29 additional volunteers are needed on day 1 Monday',
'9.05 additional volunteers are needed on day 0 Sunday',
'2.25 additional volunteers are needed on day 4 ',
'0.00 additional volunteers are needed on day 3 Wednesday' ]

//Actual
 [ '9.05 additional volunteers are needed on day 0 Sunday',
 '16.29 additional volunteers are needed on day 1 Monday',
 '26.67 additional volunteers are needed on day 2 Tuesday',
 '0.00 additional volunteers are needed on day 3 Wednesday',
 '2.25 additional volunteers are needed on day 4  ' ]

【问题讨论】:

  • 请添加未排序的数组和排序后的版本。
  • 对不起,我做了但没有正确格式化代码
  • 为什么你不能把志愿者数组作为数字而不是字符串,让排序更容易。先对数组排序,再造句子
  • 这些数字来自不同的函数,该函数计算它们并将它们存储到字符串数组中
  • 您可以使用原始volunteersindexOfvolunteers 的排序版本映射到日期

标签: javascript arrays string sorting


【解决方案1】:

var volunteers = [ '9.05', '16.29', '26.67', '0.00', '2.25' ];

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

this.results = [];
for (var i = 0; i < volunteers.length; i++) {
  var result = {
    'value':volunteers[i] , 
    'name' : volunteers[i] + " additional volunteers are needed on day " + i +" " +days[i]
  }
  this.results.push(result);
}

this.results.sort(function(a, b) {
  return a.value - b.value;
}).reverse();

var result = this.results.map(a => a.name);

console.log(result);

【讨论】:

    【解决方案2】:

    先排序:

    getResults: function(volunteers) {
      volunteers = volunteers.sort((a, b) => +a - +b);
      // Use sorted `volunteers` 
    }
    

    【讨论】:

    • 这只是对数字进行排序,虽然这是我需要的一部分,但这些数字需要将它们各自的日期保持在同一行。
    【解决方案3】:

    试试这个:

    function getNumber(string) {
      const numbers = string.match(/[0-9\.]+/g)[0] || 0
      return Number(numbers);
    }
    function sort (volunteers) {
      return volunteers.sort((a, b) => getNumber(a) - getNumber(b));
    }
    sort(volunteers)
    

    【讨论】:

      【解决方案4】:

      您可以获取索引并使用值降序对它们进行排序,并使用排序后的索引映射字符串。

      var volunteers = ['9.05', '16.29', '26.67', '0.00', '2.25'],
          days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
          result = [...volunteers.keys()]
              .sort((a, b) => volunteers[b] - volunteers[a])
              .map(i => `${volunteers[i]} additional volunteers are needed on day ${i} ${days[i]}`);
      
      console.log(result);

      【讨论】:

        【解决方案5】:

        parseFloat 可用于获取数字部分:

        var arr = [ '9.05 additional volunteers are needed on day 0 Sunday',
                   '16.29 additional volunteers are needed on day 1 Monday',
                   '26.67 additional volunteers are needed on day 2 Tuesday',
                    '0.00 additional volunteers are needed on day 3 Wednesday',
                   '2.25 additional volunteers are needed on day 4' ]
        
        console.log( arr.sort((a, b) => parseFloat(b) - parseFloat(a)) )

        【讨论】:

          猜你喜欢
          • 2015-06-28
          • 1970-01-01
          • 1970-01-01
          • 2020-03-03
          • 2014-06-29
          • 1970-01-01
          • 2021-11-08
          • 2013-06-05
          • 1970-01-01
          相关资源
          最近更新 更多