我有一个字符串数组,我需要在JavaScript中排序,但以不区分大小写的方式。如何做到这一点?
当前回答
为了支持已接受的答案,我想补充一点,下面的函数似乎改变了原始数组中的值,以便不仅对小写进行排序,而且大写值也将被更改为小写。这对我来说是一个问题,因为即使我希望看到玛丽在玛丽旁边,我也不希望第一个值玛丽的大小写改为小写。
myArray.sort(
function(a, b) {
if (a.toLowerCase() < b.toLowerCase()) return -1;
if (a.toLowerCase() > b.toLowerCase()) return 1;
return 0;
}
);
在我的实验中,下面的函数从接受的答案正确排序,但没有改变值。
["Foo", "bar"].sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
其他回答
如果你在努力理解以下内容,这可能会有所帮助:
var array = ["sort", "Me", "alphabetically", "But", "Ignore", "case"];
console.log('Unordered array ---', array, '------------');
array.sort(function(a,b) {
a = a.toLowerCase();
b = b.toLowerCase();
console.log("Compare '" + a + "' and '" + b + "'");
if( a == b) {
console.log('Comparison result, 0 --- leave as is ');
return 0;
}
if( a > b) {
console.log('Comparison result, 1 --- move '+b+' to before '+a+' ');
return 1;
}
console.log('Comparison result, -1 --- move '+a+' to before '+b+' ');
return -1;
});
console.log('Ordered array ---', array, '------------');
// return logic
/***
If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
If compareFunction(a, b) is greater than 0, sort b to a lower index than a.
***/
http://jsfiddle.net/ianjamieson/wmxn2ram/1/
用. tolowercase()规范.sort()中的case。
我把上面的答案包装在一个填充中,这样我就可以在字符串数组上调用.sortIgnoreCase()
// Array.sortIgnoreCase() polyfill
if (!Array.prototype.sortIgnoreCase) {
Array.prototype.sortIgnoreCase = function () {
return this.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
};
}
你也可以使用新的Intl.Collator().compare,每个MDN在排序数组时更有效。缺点是旧的浏览器不支持它。MDN声明Safari根本不支持它。需要验证它,因为它声明Intl。支持Collator。
当比较大量字符串时,例如对大型数组排序时,最好创建Intl。对象,并使用其compare属性提供的函数
["Foo", "bar"].sort(Intl.Collator().compare); //["bar", "Foo"]
arr.sort(function(a,b) {
a = a.toLowerCase();
b = b.toLowerCase();
if( a == b) return 0;
if( a > b) return 1;
return -1;
});
在上面的函数中,如果我们只比较小写的两个值a和b,我们将不会得到漂亮的结果。
例如,如果数组是[A, A, B, B, c, c, D, D, e, e],我们使用上面的函数,我们就得到了这个数组。它没有改变任何东西。
为了使结果为[A, A, B, B, C, C, D, D, E, E],当两个小写值相等时,我们应该再次进行比较:
function caseInsensitiveComparator(valueA, valueB) {
var valueALowerCase = valueA.toLowerCase();
var valueBLowerCase = valueB.toLowerCase();
if (valueALowerCase < valueBLowerCase) {
return -1;
} else if (valueALowerCase > valueBLowerCase) {
return 1;
} else { //valueALowerCase === valueBLowerCase
if (valueA < valueB) {
return -1;
} else if (valueA > valueB) {
return 1;
} else {
return 0;
}
}
}
推荐文章
- 在数组中获取所有选中的复选框
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类