Fork me on GitHub

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

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;
posted @ 2021-07-07 12:52  365/24/60  阅读(67)  评论(0编辑  收藏  举报