Javascript 按字母顺序比较 2 个字符串以进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10198257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparing 2 strings alphabetically for sorting purposes
提问by sameold
I'm trying to compare 2 strings alphabetically for sorting purposes. For example I want to have a boolean check like if('aaaa' < 'ab')
. I tried it, but it's not giving me correct results, so I guess that's not the right syntax. How do I do this in jquery or Javascript?
我正在尝试按字母顺序比较 2 个字符串以进行排序。例如,我想要一个像if('aaaa' < 'ab')
. 我试过了,但它没有给我正确的结果,所以我想这不是正确的语法。我如何在 jquery 或 Javascript 中做到这一点?
回答by Peter V. M?rch
You do say that the comparison is for sorting purposes. Then I suggest instead:
您确实说比较是出于排序目的。然后我建议:
"a".localeCompare("b");
It returns -1
since "a" < "b"
, 1
or 0
otherwise, like you need for Array.prototype.sort()
它返回-1
以来"a" < "b"
,1
或0
以其他方式返回,就像您需要Array.prototype.sort()
Keep in mind that sorting is locale dependent. E.g. in German, ?
is a variant of a
, so "?".localeCompare("b", "de-DE")
returns -1
. In Swedish, ?
is one of the last letters in the alphabet, so "?".localeCompare("b", "se-SE")
returns 1
.
请记住,排序取决于语言环境。例如,在德语中,?
是 的变体a
,因此"?".localeCompare("b", "de-DE")
返回-1
。在瑞典语中,?
是字母表中的最后一个字母,因此"?".localeCompare("b", "se-SE")
返回1
。
Without the second parameter to localeCompare
, the browser's locale is used. Which in my experience is never what I want, because then it'll sort differently than the server, which has a fixed locale for all users.
如果没有 to 的第二个参数localeCompare
,则使用浏览器的语言环境。根据我的经验,这从来都不是我想要的,因为那样它的排序将与服务器不同,后者对所有用户都有固定的语言环境。
回答by Lix
Lets look at some test cases - try running the following expressions in your JS console:
让我们看一些测试用例 - 尝试在您的 JS 控制台中运行以下表达式:
"a" < "b"
"aa" < "ab"
"aaa" < "aab"
All return true.
都返回真。
JavaScript compares strings character by character and "a" comes before "b" in the alphabet - hence less than.
JavaScript 逐个字符地比较字符串,并且“a”在字母表中位于“b”之前——因此小于。
In your case it works like so -
在你的情况下它是这样工作的 -
1 . "a?aaa" < "?a?b"
1 . " a?aaa" < "? a?b"
compares the first two "a" characters - all equal, lets move to the next character.
比较前两个“a”字符 - 全部相等,让我们移动到下一个字符。
2 . "a?a??aa" < "a?b??"
2 . “a? a??aa” < “a? b??”
compares the second characters "a" against "b" - whoop! "a" comes before "b". Returns true.
比较第二个字符“a”和“b”——哎呀!“a”在“b”之前。返回真。
回答by Sielu
Just remember that string comparison like "x" > "X" is case-sensitive
请记住,像 "x" > "X" 这样的字符串比较是区分大小写的
"aa" < "ab" //true
"aa" < "Ab" //false
You can use .toLowerCase()
to compare without case sensitivity.
您可以.toLowerCase()
在不区分大小写的情况下使用进行比较。
回答by Shalom Friss
"a".localeCompare("b")
should actually return -1
since a
sorts before b
"a".localeCompare("b")
实际上应该返回,-1
因为a
之前排序b
回答by Eray Xx
Lets say that we have an array of objects:
假设我们有一个对象数组:
{ name : String }
then we can sort our array as follows:
然后我们可以按如下方式对数组进行排序:
array.sort(function(a, b) {
var orderBool = a.name > b.name;
return orderBool ? 1 : -1;
});
Note: Be careful for upper letters, you may need to cast your string to lower case due to your purpose.
注意:注意大写字母,由于您的目的,您可能需要将字符串转换为小写字母。