![](/img/trans.png)
[英]Strange sorting of array when using Array.prototype.sort in Chrome
[英]What's the sorting algorithm when using Array.prototype.sort() with a compare function
我知道在使用Array.prototype.sort()
,不同的瀏覽器有自己的排序算法實現。 但我很好奇,將它與比較函數一起使用是否仍然使用與不使用比較函數相同的排序算法? 如果是,比較函數對算法的性能有影響嗎? 如果沒有,它使用什么排序算法?
場景例如
const arr = [23, 1, 5, 2, 10, 2]
arr.sort((a, b) => a - b)
是的,它是相同的排序功能。 從技術上講,當不傳遞比較函數時,它仍然使用比較函數作為默認值。 當沒有提供時,Mozilla 對它使用的比較函數有一個非常詳細的解釋: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.