更新时间:2023-12-06 15:32:16
这是因为您正在使用默认的排序算法对数字进行排序,该算法会将它们转换为字符串并按字典顺序进行排序。
It's because you're sorting the numbers with the default sorting algorithm, which converts them to a string and sorts lexicographically.
而是通过其返回值传递定义排序顺序的函数。
Instead pass a function defining a sort order via its return value.
var arr = [23,43,54,2,3,12];
console.log(arr.sort((a, b) => a - b));
返回正数会将 a
移到列表末尾。
Returning a positive number moves a
toward the end of the list.