首页 > 编程语言 > 详细

Array.sort()排序分析

时间:2021-07-07 15:12:35      阅读:27      评论:0      收藏:0      [点我收藏+]
[ 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;

Array.sort()排序分析

原文:https://www.cnblogs.com/lhx9527/p/14981054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!