我有一个字符串数组,就像这样
let array = ['Enflure','Énorme','Zimbabwe','Éthiopie','Mongolie']
我想按字母顺序排序,所以我使用array.sort(),我得到的结果是:
['Enflure','Mongolie','Éthiopie']
我想这里的重音是问题,所以我想在整个数组中用E替换É.
我试过这个
for (var i = 0; i < (array.length); i++) {
array[i].replace(/É/g,"E");
}
但它没有用.我怎么能这样做?
最佳答案
你可以使用
原文链接:https://www.f2er.com/js/429791.htmlString#localeCompare
.
The
localeCompare()
method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.The new
locales
andoptions
arguments let applications specify the language whose sort order should be used and customize the behavior of the function. In older implementations,which ignore thelocales
andoptions
arguments,the locale and sort order used are entirely implementation dependent.
var array = ['Enflure','Éthiopie'];
array.sort((a,b) => a.localeCompare(b));
console.log(array);