鍍金池/ 問答/HTML/ javacript的sort不加函數(shù)的時候是將數(shù)組里面的每個item轉(zhuǎn)換為字符串

javacript的sort不加函數(shù)的時候是將數(shù)組里面的每個item轉(zhuǎn)換為字符串,然后比較每個子字符串第一個字符的ASCII嗎?

const numArr = [2, 4, 8, 609545, 2, 8, 4, 1, 10, 123, 99];
const strArr = ["what", "We", "have", "lost", "will", "never"];
console.log(strArr.sort());
console.log(numArr.sort());

clipboard.png

回答
編輯回答
拼未來
The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

是Unicode,別的語言也需要排序。

Array.prototype.sort()

2017年7月14日 16:54
編輯回答
尤禮

根據(jù)Unicode排序的

strArr = strArr.sort((a,b)=>a-b);
2018年4月19日 15:51