我有一个字符串数组,我需要在JavaScript中排序,但以不区分大小写的方式。如何做到这一点?
当前回答
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;
}
}
}
其他回答
如果你在努力理解以下内容,这可能会有所帮助:
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/
你也可以使用Elvis操作符:
arr = ['Bob', 'charley', 'fudge', 'Fudge', 'biscuit'];
arr.sort(function(s1, s2){
var l=s1.toLowerCase(), m=s2.toLowerCase();
return l===m?0:l>m?1:-1;
});
console.log(arr);
给:
biscuit,Bob,charley,fudge,Fudge
localeCompare方法可能很好…
注意:Elvis操作符是if then else的“三元操作符”的缩写形式,通常带有赋值。 如果你从侧面看,它看起来像猫王… 例如:
if (y) {
x = 1;
} else {
x = 2;
}
你可以使用:
x = y?1:2;
也就是说,当y为真时,则返回1(用于赋值给x),否则返回2(用于赋值给x)。
ES6版本:
["Foo", "bar"].sort(Intl.Collator().compare)
来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator/compare
你也可以使用新的Intl.Collator().compare,每个MDN在排序数组时更有效。缺点是旧的浏览器不支持它。MDN声明Safari根本不支持它。需要验证它,因为它声明Intl。支持Collator。
当比较大量字符串时,例如对大型数组排序时,最好创建Intl。对象,并使用其compare属性提供的函数
["Foo", "bar"].sort(Intl.Collator().compare); //["bar", "Foo"]
在/ /i中包装字符串。这是使用正则表达式忽略大小写的一种简单方法
推荐文章
- 如何获得十六进制颜色值而不是RGB值?
- 字符串strip() JavaScript?
- 如何将一个标题转换为jQuery的URL段塞?
- 获取对象的第一个索引
- 我如何用javascript编程点击链接?
- JavaScript:客户端验证与服务器端验证
- React钩子:从回调中访问最新状态
- 使用lodash将对象转换为数组
- 打印在 JsFiddle 中
- AngularJS只适用于单页应用程序吗?
- Javascript和regex:分割字符串并保留分隔符
- 如何检查DST(日光节约时间)是否有效,如果是,偏移量?
- 如何打破_。在underscore.js中的每个函数
- 如何在jQuery中获得当前日期?
- 如何创建一个日期对象从字符串在javascript