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".

Answer from Peter V. Mørch on Stack Overflow
Top answer
1 of 5
246

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".

2 of 5
151

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 . "a​aaa" < "​a​b"

compares the first two "a" characters - all equal, lets move to the next character.

2 . "a​a​​aa" < "a​b​​"

compares the second characters "a" against "b" - whoop! "a" comes before "b". Returns true.

🌐
freeCodeCamp
freecodecamp.org › news › javascript-string-comparison-how-to-compare-strings-in-js
JavaScript String Comparison – How to Compare Strings in JS
July 1, 2022 - const string1 = "hello" const string2 = "world" const compareValue = string1.localeCompare(string2) // -1 · It gives -1 because, in the English locale, h in hello comes before w in the world (w is further down in the alphabetical order than h)
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript compare strings alphabetically | example code
JavaScript compare strings alphabetically | Example code
January 2, 2023 - Use localeCompare method compare strings alphabetically, It returns -1 since "a" < "b", 1 or 0 otherwise.
🌐
Jazz Community Site
jazz.net › dxl › html › 1885 - Comparing strings alphabetically.html
Comparing strings alphabetically
This seems like a very basic question to me, but I have not been able to find a clear answer in the dxl reference manual. I am wishing to compare two strings alphabetically. I was thinking about creating a function that uses the ASCII values of each character in the strings to compare them ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
If I have 2 strings and I want to return a value based on their alphabetical order - JavaScript - The freeCodeCamp Forum
October 15, 2019 - Given two Strings, return 1 if the first is higher in alphabetical order than the second, return -1 if the second is higher in alphabetical order than the first, and return 0 if they’re equal This is my attempt: it must be within the function sortAscending… function sortAscending(stringOne, stringTwo) { var alphabet = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]; for (var i = 0; i stringTwo[0]){ retu...
🌐
Hyperskill
hyperskill.org › learn › step › 18295
String Compare
Hyperskill is an educational platform for learning programming and software development through project-based courses, that helps you secure a job in tech. Master Python, Java, Kotlin, and more with real-world coding challenges.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › sort
Array.prototype.sort() - JavaScript | MDN
If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order.
🌐
IQCode
iqcode.com › code › javascript › javascript-compare-and-sort-strings-alphabetically
javascript compare and sort strings alphabetically Code Example
// Alphabetically const ascending = data.sort((a, b) =&gt; a[field].localeCompare(b[field])) // Descending const descending = ascending.reve...
🌐
JavaScript in Plain English
javascript.plainenglish.io › compare-strings-in-javascript-ba372650d4bf
Compare Strings in JavaScript. String comparison is an essential… | by Sameera Wijesooriya | JavaScript in Plain English
February 23, 2023 - The localeCompare() method is a more advanced way to compare strings in JavaScript. It compares two strings based on their alphabetical order, taking into account the language and the cultural context.
🌐
Academic Help
academichelp.net › coding › javascript › how-to-compare-strings.html
How to Compare Strings in Javascript: The Best Way
August 10, 2023 - The localeCompare method is particularly useful for comparing strings in different locales and languages. Additionally, mathematical operators like >, <, and === can be employed to compare strings based on their alphabetical order.
🌐
DEV Community
dev.to › aumayeung › comparing-non-english-strings-with-javascript-collators-57bf
Comparing Non-English Strings with JavaScript Collators - DEV Community
January 7, 2020 - To use a collator, we can construct a Collator object and then use its compare method. The compare method does a comparison of the alphabetical order of the entire string based on the locale.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › localeCompare
String.prototype.localeCompare() - JavaScript | MDN
The localeCompare() method of String values returns a number indicating whether this string comes before, or after, or is the same as the given string in sort order. In implementations with Intl.Collator API support, this method delegates to Intl.Collator. When comparing large numbers of strings, ...
🌐
Medium
medium.com › @rahul.tpointtech12 › comparing-strings-in-javascript-what-you-need-to-know-f62800a96d34
Comparing Strings in JavaScript: What You Need to Know | by Rahul | Medium
March 21, 2025 - Strings in JavaScript are compared lexicographically, meaning they are compared character by character based on their Unicode values. This type of comparison is useful for sorting strings alphabetically.
🌐
Reddit
reddit.com › r/learnprogramming › help comparing strings if-else
r/learnprogramming on Reddit: HELP comparing strings if-else
March 8, 2019 -

FIGURED THIS OUT WITH THE HELP OF YOU WONDERFUL PEOPLE, THANK YOU.

// Compare the given strings and display the string that comes alphabetically first.

var firstName1 = "Ann"; // Code will be tested with different names

var firstName2 = "Anthony"; // Code will be tested with different names

if ("firstName1"<"firstName2"){console.log(firstName1)}

else {console.log(firstName2)}

there are 4 other names the code is tested with including "Ron" & "Robert", and "Veronica Barret" & "Veronica Burton"

I get the Correct output for Ann/Anthony, Veronica Barret/Veronica Burton. For Ron/Robert, it is displaying Ron instead of Robert. It seems like rather than comparing firstName1 and firstName2 and outputting the string that comes alphabetically first, this code is just outputting firstName1...Exhausted everything I can do, really could use some help lol. Please let me know if there is something I forgot to include...

🌐
Linux Hint
linuxhint.com › compare-strings-in-javascript
How to Compare Strings in JavaScript
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-a-string-alphabetically-using-a-function-in-javascript
JavaScript - Sort a String Alphabetically using a Function - GeeksforGeeks
July 23, 2025 - This is the most basic and commonly used method to sort a string alphabetically. The string is first converted into an array of characters, sorted, and then joined back into a string. ... The spread operator can be used to split the string, and localeCompare() provides a locale-aware sorting method. ... const s = "javascript"; const sorted = [...s].sort((a, b) => a.localeCompare(b)).join(""); console.log(sorted); ... This approach is helpful if locale-aware comparisons are needed (e.g., for accented characters).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-localecompare-method
JavaScript String localeCompare() Method - GeeksforGeeks
July 11, 2025 - Example 2: This example shows the basic use of the string.localeCompare() Method in Javascript. ... // An alphabet "n" comes before "z" which // gives a negative value let a = 'n'.localeCompare('z'); console.log(a) // Alphabetically the word ...