我在 JS 中有这个对数组:
var my_array = [['line_1','2'],['line_2','0'],['line_3','5'],['line_4',''],['line_5','4'],]
现在按我正在使用的第二个项目对其进行排序:
var my_sorted_array =my_array.sort((a, b) => a[1] - b[1])
但我想把空白/空和空值放在最后。
预期结果:
[['line_1','2'],['line_5','4'],['line_3','5'],['line_2','0']['line_4','']]
谢谢
在进行排序比较时,您可以将每个
0
值替换为 Infinity
:
let a = [['line_1','2'],['line_2','0'],['line_3','5'],['line_4',''],['line_5','4']];
a.sort(([,a],[,b])=>(+a||Infinity)-(+b||Infinity));
console.log(a);