【发布时间】: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 ' ]
【问题讨论】:
-
请添加未排序的数组和排序后的版本。
-
对不起,我做了但没有正确格式化代码
-
为什么你不能把志愿者数组作为数字而不是字符串,让排序更容易。先对数组排序,再造句子
-
这些数字来自不同的函数,该函数计算它们并将它们存储到字符串数组中
-
您可以使用原始
volunteers的indexOf将volunteers的排序版本映射到日期
标签: javascript arrays string sorting