我有一个对象列表,我希望基于类型字符串的字段attr进行排序。我试着用

list.sort(function (a, b) {
    return a.attr - b.attr
})

但发现-似乎不适用于JavaScript中的字符串。我如何能排序基于类型字符串的属性的对象列表?


当前回答

更新答案(2014年10月)

我真的很讨厌这个字符串自然排序顺序,所以我花了一些时间来调查这个问题。

长话短说

localeCompare()字符支持是坏蛋,使用它。 正如Shog9所指出的,你的问题的答案是:

return item1.attr.localeCompare(item2.attr);

在所有自定义JavaScript“自然字符串排序顺序”实现中发现的错误

有相当多的自定义实现,试图进行字符串比较更精确地称为“自然字符串排序顺序”

当“玩”这些实现时,我总是注意到一些奇怪的“自然排序顺序”选择,或者说是错误(最好的情况下是遗漏)。

通常,特殊字符(空格、破折号、&号、括号等)不会被正确处理。

然后你会发现它们出现在不同的地方,通常情况下可能是:

有些介于大写字母Z和小写字母a之间 有些介于9和大写的A之间 有些会在小写z后面

当人们期望所有特殊字符都“分组”在一个地方时,除了空格特殊字符maybe(它总是第一个字符)。也就是说,要么全部在数字之前,要么全部在数字和字母之间(小写字母和大写字母一个接一个“在一起”),要么全部在字母之后。

我的结论是,当我开始添加几乎不寻常的字符(即带有变音符的字符或像破折号、感叹号等字符)时,它们都不能提供一致的顺序。

自定义实现研究:

Natural Compare Lite https://github.com/litejs/natural-compare-lite : Fails at sorting consistently https://github.com/litejs/natural-compare-lite/issues/1 and http://jsbin.com/bevututodavi/1/edit?js,console, basic Latin characters sorting http://jsbin.com/bevututodavi/5/edit?js,console Natural Sort https://github.com/javve/natural-sort : Fails at sorting consistently, see issue https://github.com/javve/natural-sort/issues/7 and see basic Latin characters sorting http://jsbin.com/cipimosedoqe/3/edit?js,console JavaScript Natural Sort https://github.com/overset/javascript-natural-sort: seems rather neglected since February 2012, Fails at sorting consistently, see issue https://github.com/overset/javascript-natural-sort/issues/16 Alphanum http://www.davekoelle.com/files/alphanum.js , Fails at sorting consistently, see http://jsbin.com/tuminoxifuyo/1/edit?js,console

通过localeCompare()实现浏览器的本地“自然字符串排序顺序”

localeCompare()最老的实现(没有locale和options参数)被Internet Explorer 6及更高版本所支持,请参阅http://msdn.microsoft.com/en-us/library/ie/s4esdbwz(v=vs.94).aspx(向下滚动到localeCompare()方法)。

内置的localeCompare()方法在排序方面做得更好,甚至是国际字符和特殊字符。

使用localeCompare()方法的唯一问题是“使用的语言环境和排序顺序完全依赖于实现”。换句话说,当使用localeCompare(如stringOne.localeCompare(stringTwo))时:Firefox、Safari、Chrome和Internet Explorer对字符串有不同的排序顺序。

浏览器原生实现研究:

http://jsbin.com/beboroyifomu/1/edit?js,console - basic Latin characters comparison with localeCompare() http://jsbin.com/viyucavudela/2/ - basic Latin characters comparison with localeCompare() for testing on Internet Explorer 8 http://jsbin.com/beboroyifomu/2/edit?js,console - basic Latin characters in string comparison : consistency check in string vs when a character is alone https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare - Internet Explorer 11 and later supports the new locales & options arguments

“字符串自然排序顺序”的难点

实现一个可靠的算法(意思是:一致但也涵盖广泛的字符)是一项非常艰巨的任务。UTF-8包含2000多个字符,涵盖120多种脚本(语言)。

最后,这个任务有一些规范,它被称为“Unicode Collation Algorithm”,可以在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);

进一步阅读:

https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order How to sort strings in JavaScript Natural sort of alphanumerical strings in JavaScript Sort Array of numeric & alphabetical elements (Natural Sort) Sort mixed alpha/numeric array https://web.archive.org/web/20130929122019/http://my.opera.com/GreyWyvern/blog/show.dml/1671288 https://web.archive.org/web/20131005224909/http://www.davekoelle.com/alphanum.html http://snipplr.com/view/36012/javascript-natural-sort/ http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/

多亏了Shog9的回答,让我找到了我相信的“正确”方向。

其他回答

嵌套三元箭头函数

(a,b) => (a < b ? -1 : a > b ? 1 : 0)

直接使用sort(),不带任何-或<

Const区域=['山','海滩','沙漠','山'] console.log (areas.sort ()) //以降序方式打印 .reverse console.log (areas.sort () ())

更新答案(2014年10月)

我真的很讨厌这个字符串自然排序顺序,所以我花了一些时间来调查这个问题。

长话短说

localeCompare()字符支持是坏蛋,使用它。 正如Shog9所指出的,你的问题的答案是:

return item1.attr.localeCompare(item2.attr);

在所有自定义JavaScript“自然字符串排序顺序”实现中发现的错误

有相当多的自定义实现,试图进行字符串比较更精确地称为“自然字符串排序顺序”

当“玩”这些实现时,我总是注意到一些奇怪的“自然排序顺序”选择,或者说是错误(最好的情况下是遗漏)。

通常,特殊字符(空格、破折号、&号、括号等)不会被正确处理。

然后你会发现它们出现在不同的地方,通常情况下可能是:

有些介于大写字母Z和小写字母a之间 有些介于9和大写的A之间 有些会在小写z后面

当人们期望所有特殊字符都“分组”在一个地方时,除了空格特殊字符maybe(它总是第一个字符)。也就是说,要么全部在数字之前,要么全部在数字和字母之间(小写字母和大写字母一个接一个“在一起”),要么全部在字母之后。

我的结论是,当我开始添加几乎不寻常的字符(即带有变音符的字符或像破折号、感叹号等字符)时,它们都不能提供一致的顺序。

自定义实现研究:

Natural Compare Lite https://github.com/litejs/natural-compare-lite : Fails at sorting consistently https://github.com/litejs/natural-compare-lite/issues/1 and http://jsbin.com/bevututodavi/1/edit?js,console, basic Latin characters sorting http://jsbin.com/bevututodavi/5/edit?js,console Natural Sort https://github.com/javve/natural-sort : Fails at sorting consistently, see issue https://github.com/javve/natural-sort/issues/7 and see basic Latin characters sorting http://jsbin.com/cipimosedoqe/3/edit?js,console JavaScript Natural Sort https://github.com/overset/javascript-natural-sort: seems rather neglected since February 2012, Fails at sorting consistently, see issue https://github.com/overset/javascript-natural-sort/issues/16 Alphanum http://www.davekoelle.com/files/alphanum.js , Fails at sorting consistently, see http://jsbin.com/tuminoxifuyo/1/edit?js,console

通过localeCompare()实现浏览器的本地“自然字符串排序顺序”

localeCompare()最老的实现(没有locale和options参数)被Internet Explorer 6及更高版本所支持,请参阅http://msdn.microsoft.com/en-us/library/ie/s4esdbwz(v=vs.94).aspx(向下滚动到localeCompare()方法)。

内置的localeCompare()方法在排序方面做得更好,甚至是国际字符和特殊字符。

使用localeCompare()方法的唯一问题是“使用的语言环境和排序顺序完全依赖于实现”。换句话说,当使用localeCompare(如stringOne.localeCompare(stringTwo))时:Firefox、Safari、Chrome和Internet Explorer对字符串有不同的排序顺序。

浏览器原生实现研究:

http://jsbin.com/beboroyifomu/1/edit?js,console - basic Latin characters comparison with localeCompare() http://jsbin.com/viyucavudela/2/ - basic Latin characters comparison with localeCompare() for testing on Internet Explorer 8 http://jsbin.com/beboroyifomu/2/edit?js,console - basic Latin characters in string comparison : consistency check in string vs when a character is alone https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare - Internet Explorer 11 and later supports the new locales & options arguments

“字符串自然排序顺序”的难点

实现一个可靠的算法(意思是:一致但也涵盖广泛的字符)是一项非常艰巨的任务。UTF-8包含2000多个字符,涵盖120多种脚本(语言)。

最后,这个任务有一些规范,它被称为“Unicode Collation Algorithm”,可以在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);

进一步阅读:

https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order How to sort strings in JavaScript Natural sort of alphanumerical strings in JavaScript Sort Array of numeric & alphabetical elements (Natural Sort) Sort mixed alpha/numeric array https://web.archive.org/web/20130929122019/http://my.opera.com/GreyWyvern/blog/show.dml/1671288 https://web.archive.org/web/20131005224909/http://www.davekoelle.com/alphanum.html http://snipplr.com/view/36012/javascript-natural-sort/ http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/

多亏了Shog9的回答,让我找到了我相信的“正确”方向。

var str = ['v','a','da','c','k','l']
var b = str.join('').split('').sort().reverse().join('')
console.log(b)

我为这个问题困扰了很久,所以我最终研究了这个问题,并给了你一个冗长的原因,为什么事情是这样的。

来自规范:

Section 11.9.4   The Strict Equals Operator ( === )

The production EqualityExpression : EqualityExpression === RelationalExpression
is evaluated as follows: 
- Let lref be the result of evaluating EqualityExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating RelationalExpression.
- Let rval be GetValue(rref).
- Return the result of performing the strict equality comparison 
  rval === lval. (See 11.9.6)

现在我们看11.9.6

11.9.6   The Strict Equality Comparison Algorithm

The comparison x === y, where x and y are values, produces true or false. 
Such a comparison is performed as follows: 
- If Type(x) is different from Type(y), return false.
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is Number, then
...
- If Type(x) is String, then return true if x and y are exactly the 
  same sequence of characters (same length and same characters in 
  corresponding positions); otherwise, return false.

就是这样。应用于字符串的三重等号运算符如果参数是完全相同的字符串(相同的长度和对应位置的相同字符)则返回true。

所以===将在我们试图比较可能来自不同来源的字符串,但我们知道它们最终将具有相同的值的情况下工作——这是我们代码中内联字符串的一个足够常见的场景。例如,如果我们有一个名为connection_state的变量,并且我们希望知道它现在处于以下状态中的哪一种['connecting', 'connected', 'disconnecting', 'disconnected'],我们可以直接使用===。

但还有更多。就在11.9.4上面,有一个简短的说明:

NOTE 4     
  Comparison of Strings uses a simple equality test on sequences of code 
  unit values. There is no attempt to use the more complex, semantically oriented
  definitions of character or string equality and collating order defined in the 
  Unicode specification. Therefore Strings values that are canonically equal
  according to the Unicode standard could test as unequal. In effect this 
  algorithm assumes that both Strings are already in normalized form.

嗯。现在该做什么?外部获得的字符串可能,而且很可能会是奇怪的统一码,而我们温和的===不会公正地对待它们。在localeCompare来拯救:

15.5.4.9   String.prototype.localeCompare (that)
    ...
    The actual return values are implementation-defined to permit implementers 
    to encode additional information in the value, but the function is required 
    to define a total ordering on all Strings and to return 0 when comparing
    Strings that are considered canonically equivalent by the Unicode standard. 

我们现在可以回家了。

tl; diana;

要比较javascript中的字符串,请使用localeCompare;如果您知道字符串没有非ascii组件,因为它们是,例如,内部程序常量,那么===也适用。