我有这个功能,但它没有正确排序我的百分比:
var arrayWithTheSortedPrice = [10, 12, 18.5, -56, -5, -12.5];
arrayWithTheSortedPrice.sort(function(a, b) {
return a[1] < b[1] ? 1 : -1;
});
console.log(arrayWithTheSortedPrice)Run Code Online (Sandbox Code Playgroud)
它是这样的:
[18.5,12,10,-56,-12.5,-5]
Run Code Online (Sandbox Code Playgroud)
我想要这个结果:
[18.5,12,10,-5,-12.5,-56]
Run Code Online (Sandbox Code Playgroud)
你的排序功能有点偏.你很容易用数字做到这一点......
var arrayWithTheSortedPrice = [10, 12, 18.5, -56, -5, -12.5];
arrayWithTheSortedPrice.sort(function(a, b) {
return b - a;
});
console.log(arrayWithTheSortedPrice)Run Code Online (Sandbox Code Playgroud)
sort函数需要一个负值,一个正值或零,然后根据该结果决定如何对数组进行排序.这只是说按降序排序数字.
| 归档时间: |
|
| 查看次数: |
488 次 |
| 最近记录: |