简体   繁体   English

按字母顺序对数组进行排序

[英]Sort array alphabetically javascript

quick question. 快速提问。 Does the .sort method in javascript automatically sort items in an array using ASCII order. javascript中的.sort方法是否使用ASCII顺序自动对数组中的项目进行排序。 I've looked at examples etc but haven't seen a definitive answer anywhere. 我看过示例,但在任何地方都没有找到明确的答案。 Also what alternative ways of alphabetically sorting an array is there? 还有哪些按字母顺序对数组排序的替代方法?

Thanks in advance 提前致谢

Yes. 是。 If you look at the .sort() method on MDN it states the case when the compare function parameter is omitted: 如果您在MDN上查看.sort()方法,则会说明省略了compare函数参数的情况:

Specifies a function that defines the sort order. 指定定义排序顺序的函数。 If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element. 如果省略,则根据每个字符的Unicode代码点值,以及每个元素的字符串转换,对数组进行排序。

Since Unicode is a super set of ASCII then yes it does sort in ASCII order. 由于Unicode是ASCII的超集,因此可以,它的确按ASCII顺序排序。

Unless you are using "ASCII" like "Kleenex" (vs facial tissue), no, nothing in JavaScript is ASCII. 除非您使用像“面巾纸”(相对于面巾纸)之类的“ ASCII”,否则,JavaScript中没有什么是ASCII。 In particular, strings are counted sequences of UTF-16 code units, one or two of which encode a Unicode codepoint. 特别是,字符串是计数的UTF-16代码单元序列,其中一个或两个编码Unicode代码点。 (This applies to Java, .NET, JavaScript, HTML, XML,….) (这适用于Java,.NET,JavaScript,HTML,XML等。)

A lexicographic ordering is not very useful for human-valued text. 字典编排顺序对于以人为本的文本不是很有用。 Since you ask about alphabetically sorting, you must have a particular language's writing system's alphabet in mind. 由于您询问的是字母排序,因此您必须牢记特定语言的书写系统的字母。 So, perhaps you want to pass localeCompare to sort . 因此,也许您想通过localeCompare进行sort

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM