现在,这在使用localeCompare的现代浏览器中成为可能。通过传递
numeric:true选项,它将智能识别数字。您可以使用进行不区分大小写的操作
sensitivity:'base'。已在Chrome,Firefox和IE11中测试。
这是一个例子。返回
1,表示10之后是2:
'10'.localeCompare('2', undefined, {numeric: true, sensitivity: 'base'})为了提高排序大量字符串时的性能,本文说:
比较大量字符串时,例如在对大型数组进行排序时,最好创建一个Intl.Collator对象,并使用其compare属性提供的功能。文件连结
var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});var myArray = ['1_document', '11_document', '2_document'];console.log(myArray.sort(collator.compare));


