我有一个对象列表,希望根据string类型的字段attr进行排序。我试着使用-
list.sort(function (a, b) {
return a.attr - b.attr
})但是发现在JavaScript中-似乎不能处理字符串。如何根据类型为string的属性对对象列表进行排序?
发布于 2008-09-09 03:29:17
根据您的示例使用String.prototype.localeCompare a:
list.sort(function (a, b) {
return ('' + a.attr).localeCompare(b.attr);
})我们强制a.attr为字符串以避免异常。since Internet Explorer 6和Firefox1都支持localeCompare。您可能还会看到使用的以下代码不符合区域设置:
if (item1.attr < item2.attr)
return -1;
if ( item1.attr > item2.attr)
return 1;
return 0;发布于 2014-10-10 16:39:43
更新的答案(2014年10月)
我真的对这种字符串的自然排序顺序感到恼火,所以我花了相当长的时间来研究这个问题。我希望这能帮到你。
长话短说
localeCompare()字符支持很糟糕,使用它就行了。正如Shog9所指出的,你的问题的答案是:
return item1.attr.localeCompare(item2.attr);在所有自定义javascript“自然字符串排序顺序”实现中发现的错误
有相当多的自定义实现,试图进行字符串比较,更准确地称为“自然字符串排序顺序”。
当“玩”这些实现时,我总是注意到一些奇怪的“自然排序”选择,或者更确切地说是错误(或者在最好的情况下是省略)。
通常,不会正确处理特殊字符(空格、破折号、与号、方括号等)。
然后你会发现它们在不同的地方混合在一起,通常是:
之后
当人们期望所有特殊字符都被“分组”在一个地方时,除了空格特殊字符可能(它总是第一个字符)。也就是说,要么都在数字之前,要么都在数字和字母之间(小写和大写是一个接一个地“在一起”),或者都在字母之后。
我的结论是,当我开始添加几乎不寻常的字符时,它们都无法提供一致的顺序(即。带有变音符号或字符的字符,如破折号、感叹号等)。
对自定义实现的研究:
Natural Compare Lite https://github.com/litejs/natural-compare-lite:Fails at sorting consistently https://github.com/litejs/natural-compare-lite/issues/1 http://jsbin.com/bevututodavi/1/edit?js,console,基本拉丁字符排序和排序失败:无法一致排序,请参见issue http://jsbin.com/bevututodavi/5/edit?js,console Javascript Natural Sort https://github.com/javve/natural-sort/issues/7 http://jsbin.com/cipimosedoqe/3/edit?js,console Javascript Natural Sorthttp://jsbin.com/cipimosedoqe/3/edit?js,console https://github.com/overset/javascript-natural-sort:似乎自2012年2月以来一直被忽视,无法一致排序,请参见issue https://github.com/overset/javascript-natural-sort/issues/16Alphanum http://www.davekoelle.com/files/alphanum.js,无法一致排序,请参见http://jsbin.com/tuminoxifuyo/1/edit?js,console浏览器的原生“自然字符串排序顺序”通过localeCompare()实现
IE6+支持localeCompare()最早的实现(没有区域设置和选项参数),请参阅http://msdn.microsoft.com/en-us/library/ie/s4esdbwz(v=vs.94).aspx (向下滚动到localeCompare()方法)。内置的localeCompare()方法在排序方面做得更好,甚至对国际字符和特殊字符也是如此。使用localeCompare()方法的唯一问题是"the locale and sort order used are entirely implementation dependent". In other words, when using localeCompare such as stringOne.localeCompare(stringTwo): Firefox, Safari, Chrome & IE have a different sort order for Strings.
浏览器原生实现的研究:
“字符串自然排序”的难度
实现一个可靠的算法(意思是:一致,但也涵盖了广泛的字符)是一项非常艰巨的任务。UTF8包含more than 2000 characters & covers more than 120 scripts (languages)。最后,有一些关于这个任务的规范,它被称为"Unicode排序算法“,可以在http://www.unicode.org/reports/tr10/上找到。你可以在我发布的https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order上的这个问题上找到更多的信息
最终结论
因此,考虑到我遇到的javascript自定义实现所提供的当前级别的支持,我们可能永远看不到任何东西能够接近支持所有这些字符和脚本(语言)。因此,我宁愿使用浏览器的本机localeCompare()方法。是的,它确实有在不同浏览器上不一致的缺点,但基本的测试表明,它涵盖了更广泛的字符范围,允许固定和有意义的排序顺序。
因此,正如Shog9所指出的,对您的问题的答案是:
return item1.attr.localeCompare(item2.attr);进一步阅读:
感谢shog9的回答,我相信这让我走到了正确的方向。
发布于 2016-11-01 14:28:03
答案(在现代ECMAScript中)
list.sort((a, b) => (a.attr > b.attr) - (a.attr < b.attr))或
list.sort((a, b) => +(a.attr > b.attr) || -(a.attr < b.attr))描述
将布尔值转换为数字会产生以下结果:
true 1false 0-> ->
考虑三种可能的模式:
1
0
-1
is y:(x > y) - (y < x) -> 1 - 0 -> -1 is -> to y:(x > y) - (y < x) -> 1 - 0 -> (x > y) - (y < x) -> 0 - 0 -> y:(x > y) - (y < x) -> 0 - 1 ->->
(可选)
1
0
-1
is y:+(x > y) || -(x < y) -> 1 || 0 -> -1 is -> to y:+(x > y) || -(x < y) -> 1 || 0 -> +(x > y) || -(x < y) -> 0 || 0 -> y:+(x > y) || -(x < y) -> 0 || -1 ->->
因此,这些逻辑等价于典型的排序比较器函数。
if (x == y) {
return 0;
}
return x > y ? 1 : -1;https://stackoverflow.com/questions/51165
复制相似问题