JavaScript日记 #1期:js中容易搞混的比较函数
sort 🔗︎
sort方法会改变原数组
var a = [1,4,2,3]
a.sort()
console.log(a)
toSorted 🔗︎
toSorted不会改变原数组
var a = [1,4,2,3]
a.toSorted()
console.log(a)
localCompare 🔗︎
主要对比字符串
const a = 'réservé'; // With accents, lowercase
const b = 'RESERVE'; // No accents, uppercase
console.log(a.localeCompare(b));
// Expected output: -1
console.log(a.localeCompare(b, 'en', { sensitivity: 'base' }));
// Expected output: 0
'1'.localeCompare('1')
// 0