其他分享
首页 > 其他分享> > Array.sort()排序分析

Array.sort()排序分析

作者:互联网

[ 10, 12, 14, 16, 8 ].sort()
// 输出 :[10, 12, 14, 16, 8]

查看 sort源码 :不传参的话会逐个比较 ASCLL 值,而数字8的ASCLL值大于1的ASCLL值。修改:[ 10, 12, 14, 16, 8 ].sort((a,b)=> a-b)

/**
* Sorts an array.
* @param compareFn Function used to determine the order of the elements. It is expected to return
* a negative value if first argument is less than second argument, zero if they're equal and a positive
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
* ```ts
* [11,2,22,1].sort((a, b) => a - b)
* ```
*/
sort(compareFn?: (a: number, b: number) => number): this;

标签:sort,10,12,14,number,ASCLL,Array,排序
来源: https://www.cnblogs.com/lhx9527/p/14981054.html