javascript - NodeJS 14.4.0 使用什么排序算法对四个元素的数组进行 Array.sort
问题描述
我不相信这是这个的副本 Javascript 的 sort() 是如何工作的?
let arr = [3, 4, 2, 1];
arr.sort((second,first) => {
console.log([first, second]);
if (first>second) {
return -1; // switch them
}
return 0; // don't switch them
});
console.log(arr);
这返回
[ 3, 4 ]
[ 4, 2 ]
[ 4, 2 ] <---- Why is this output twice?
[ 3, 2 ]
[ 3, 1 ]
[ 2, 1 ]
[ 1, 2, 3, 4 ]
我想弄清楚 NodeJS (14.4.0) 使用我的输入对 Array.sort 使用什么算法?
解决方案
正如这篇文章中的注释,v8 引擎显然使用 Timsort 进行排序:
推荐阅读
- angular - 如何在角度 9 中使用模板驱动形式设置输入值
- python - 过滤列表的元素
- c# - 使用 Linq 的 Dense Rank C# DataTable
- assembly - printf完成后如何让程序以0退出?
- sql-server - SQL Server 的 ASP.NET Core 3.1 数据库上下文添加十进制值,结果是该值的 10 倍
- php - 在magento 2中更改产品的Url键时,您如何不替换/被 -
- javascript - 每次页面刷新时更改类名
- python - 导出到 csv 文件时出现 OSError Invalid arg 错误
- javascript - Discord Bot 自动发送消息
- verilog - 如何防止 Verilog 中的推断锁存和锁存不安全行为?