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?

- 18,400
- 21
- 63
- 87
-
2Have you seen http://stackoverflow.com/questions/1134976/how-may-i-sort-a-list-alphabetically-using-jquery? – j08691 Apr 17 '12 at 20:02
-
That's how you do it. What result do you expect? The expression `'aaaa' < 'ab'` returns `true`. <! -- false edit to remove vote --> – Guffa Apr 17 '12 at 20:03
5 Answers
You do say that the comparison is for sorting purposes. Then I suggest localeCompare instead:
"a".localeCompare("b");
It returns -1
since "a" < "b"
, 1
or 0
otherwise, like you need for 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", "sv-SE")
returns 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.
Also, if what you are sorting contains numbers, you may want:
"a5b".localeCompare("a21b", undefined, { numeric: true })
This returns -1, recognizing that 5 as a number is less than 21. Without { numeric: true }
it returns 1, since "2" sorts before "5". In many real-world applications, users expect "a5b" to come before "a21b".

- 8,409
- 22
- 75
- 99

- 13,830
- 8
- 69
- 103
-
7+1 also worth mentioning that this is fine for cases too (`"aa".localeCompare("ab")` == "aa".localeCompare("Ab") for any locale I can think of) and works even in old versions of IE. Should be the accepted answer! – user56reinstatemonica8 Aug 24 '16 at 00:33
-
1+1 for Peter; This is more compatible with how Javascript array sorting works anyway so it's more useful, and I think it's more of what the OP was asking for. – Vee Dec 26 '16 at 20:24
-
I suppose, this should be the right answer. The only way to make alphabetical (**not Unicode**) strings comparation. – Limbo Apr 17 '19 at 21:44
-
This seemed to really do the trick, even for alphanumeric values. Thanks. – Joshua Pinter Oct 01 '19 at 15:19
Lets look at some test cases - try running the following expressions in your JS console:
"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.
In your case it works like so -
1 . "aaaa" < "ab"
compares the first two "a" characters - all equal, lets move to the next character.
2 . "aaaa" < "ab"
compares the second characters "a" against "b" - whoop! "a" comes before "b". Returns true.

- 47,311
- 12
- 103
- 131
-
2for discussion about special characters such as Č,č,Š,š,Ž,ž, see http://stackoverflow.com/questions/6909126/javascript-sort-with-unicode – dsdsdsdsd Aug 05 '13 at 00:38
-
4
-
17Something to keep in mind would be capitals. `"a" < "b" === true` `"a" < "B" === false`. – Jan 22 '17 at 13:14
-
1
-
1
Just remember that string comparison like "x" > "X" is case-sensitive
"aa" < "ab" //true
"aa" < "Ab" //false
You can use .toLowerCase()
to compare without case sensitivity.

- 13,251
- 5
- 38
- 63

- 1,070
- 14
- 19
Let's say we have an array of objects such as:
{ name: String }
then we can sort our array as follows:
array.sort((a, b) => {
if (a.name === b.name) return 0;
return a.name > b.name ? 1 : -1;
});
Note: Be careful with uppercase letters. You may need to cast your string to lowercase depending on the purpose.

- 6,738
- 2
- 42
- 45

- 183
- 1
- 9
-
1The function used for sorting is supposed to return 0 if they are equal. This function only returns -1 or 1. – Steve Jun 25 '20 at 15:04
-
@Steve - well, *pragmatically* speaking, this is ok as nothing will change for the user. That said, I agree - this is not a spec-compliant comparator which leads to unnecessary swaps of equal strings. Won't be noticeable on small (or unique enough) arrays, but can slow things down on large collections with mostly identical elements. – Oleg Valter is with Ukraine Nov 08 '20 at 14:00
-
A good point here is that a direct comparison returns a boolean, which is not enough to operate an array sorting and will actually lead to incorrect results. For sorting, one has to return 0, 1 or -1. – Victor Schröder May 31 '22 at 14:05
"a".localeCompare("b")
should actually return -1
since a
sorts before b

- 28,700
- 8
- 66
- 83

- 165
- 1
- 3
-
1If you meant this as a comment to my answer, you're right. My answer used to incorrectly show the result as 1, so I corrected that. – Peter V. Mørch Dec 12 '16 at 23:21