小编Nit*_*Kio的帖子

Can any one explain this strange behavior about Array sort?

I have a array like this

a = [0, "-", "-", "-", "-", "-", -0.2, -0.05, 0.25, 0.47, 0.875].

notice that there are some '-' in this array and only this string appears and the left are numbers.

now I apply sort on this array, just like

a = [0, "-", "-", "-", "-", "-", -0.2, -0.05, 0.25, 0.47, 0.875];
console.log(a.sort((a, b) => (+a || -Infinity) - (+b || -Infinity)));
Run Code Online (Sandbox Code Playgroud)

or

a = [0, "-", "-", "-", "-", "-", …
Run Code Online (Sandbox Code Playgroud)

javascript arrays sorting

3
推荐指数
1
解决办法
91
查看次数

标签 统计

arrays ×1

javascript ×1

sorting ×1