快速的问题。 javascript中的.sort方法是否使用ASCII顺序自动对数组中的项进行排序。 我看了一些例子,但没有看到任何确定的答案。 还有什么替代方法按字母顺序排序数组?
提前致谢
quick question. Does the .sort method in javascript automatically sort items in an array using ASCII order. 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
最满意答案
是。 如果你在MDN上查看.sort()方法,它会说明省略compare函数参数的情况:
指定定义排序顺序的函数。 如果省略,则根据每个元素的字符串转换,根据每个字符的Unicode代码点值对数组进行排序。
由于Unicode是一组超级ASCII,因此它按ASCII顺序排序。
Yes. If you look at the .sort() method on MDN it states the case when the compare function parameter is omitted:
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.
Since Unicode is a super set of ASCII then yes it does sort in ASCII order.
更多推荐
发布评论