what about

return Character.toLowerCase(ch1) < Character.toLowerCase(ch2);
Answer from aleph_null on Stack Overflow
🌐
Quora
quora.com › How-do-you-compare-strings-alphabetically-in-Java
How to compare strings alphabetically in Java - Quora
Comparing strings alphabetically in Java relies on the String API and locale-aware collators when needed. Below are the primary techniques, when to use each, and code examples. ... Behavior: lexicographical comparison based on Unicode code units (UTF-16). Returns an int: ... Use when you need a simple, deterministic Unicode code unit ordering (e.g., internal keys, dictionaries). ... Note: compareTo is case-sensitive and treats characters by their numeric code units (so "Z" < "a").
Discussions

java - Comparing strings by their alphabetical order - Stack Overflow
String s1 = "Project"; String s2 = "Sunject"; I want to compare the two above string by their alphabetic order (which in this case "Project" then "Sunject" as "P" comes before "S"). Does anyone kno... More on stackoverflow.com
🌐 stackoverflow.com
How to compare characters in two strings to sort them alphabetically? (without c string library functions)
What does "sort alphabetically" mean? Typically, we compare strings lexicographically. Is alphabetically supposed to mean the same thing? Judging by your code, you make instant decision if the lengths of the strings differ. This is inconsistent with lexicographical comparison. In lexicographical comparison lengths of the strings are secondary. In any case, what were you trying to do (even in Java) with this comparison if (length[i] < length[i]-1) How can length[i] possibly be smaller than length[i]-1??? E.g. how can, say, 5 be smaller than 4? More on reddit.com
🌐 r/C_Programming
1
1
February 10, 2015
[Intro Comp. Sci.] Sorting an ArrayList of Strings Alphabetically in Java?

Collections.sort uses the natural ordering of objects in the ArrayList, which for String objects is Lexicographically. Collections.sort( ArrayList < String > ) should work the way you want.

More on reddit.com
🌐 r/HomeworkHelp
3
2
December 10, 2012
How to compare two strings alphabetically without using "compareTo"
You should try it and see what you get. That's the easiest way to answer these sorts of questions. More on reddit.com
🌐 r/learnprogramming
13
0
February 26, 2018
🌐
Mkyong
mkyong.com › home › java › java string compareto() examples
Java String compareTo() examples - Mkyong.com
January 24, 2022 - In simple words, for lexicographical or alphabetical order, "a" comes before the "c" and returns a negative integer. System.out.println("a".compareTo("b")); // -1 System.out.println("a".compareTo("c")); // -2 System.out.println("a".compareTo("d")); ...
🌐
Coderanch
coderanch.com › t › 702035 › java › compare-strings-find-alphabetically-Java
How to compare two strings and find which is first alphabetically in Java? Built-in Method? (Java in General forum at Coderanch)
I'm trying to write a code that will compare two Strings to determine if the first word should be placed before or after the second word in the dictionary (alphabetically). Is there a built in method (or any sort of function, really) in Java that can do this? I'm working mostly with if-else statements right now, so I thought maybe a statement like this would work? Essentially, in the following code I was trying to isolate, for example, the first character ...
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › compareTo
Java String compareTo() - Compare Strings Alphabetically | Vultr Docs
May 15, 2025 - In this article, you will learn how to use the compareTo() method in Java to compare strings alphabetically.
🌐
W3Docs
w3docs.com › java
Comparing strings by their alphabetical order
To compare strings by their alphabetical order in Java, you can use the compareTo() method of the String class. The compareTo() method compares two strings lexicographically, which means that it compares the strings character by character, according ...
🌐
Sololearn
sololearn.com › en › Discuss › 1691307 › how-to-know-if-the-first-letter-of-a-string-is-alphabetically-greater-than-the-first-letter-of
How to know if the first letter of a String is alphabetically greater than the first letter of another String? | Sololearn: Learn to code for FREE!
February 16, 2019 - Leo Hunter But you have to be careful: first.charAt (0) > second.charAt (0) means not that you compare the letters itself, you compare the ascii values of these letters. A > b --> true (65 < 98) a < B --> false (97 > 66) If you have capital letters in your String, you can use this method: YourString.toLowerCase (); ... did old java not have an easier way?
Find elsewhere
🌐
Coderanch
coderanch.com › t › 399908 › java › Comparing-character-strings-alphabetically
Comparing two-character strings alphabetically. (Beginning Java forum at Coderanch)
I've managed to get it to work, more or less, by breaking the string apart using .charAt() and then using Character.valueOf(), and comparing the unicode, but it's ugly and I'm fairly certain that there has to be a better way that I'm overlooking. (Think of them as representing days, and I'm ...
🌐
Medium
medium.com › @AlexanderObregon › sorting-text-alphabetically-with-java-code-177cfc072947
Sorting Text Alphabetically with Java Code | Medium
July 26, 2025 - Even though “albert” starts with the same letter as “Alex”, the lowercase ‘a’ has a higher value than the uppercase ‘A’. This behavior is built into how compareTo works and can lead to sorted results that feel off if you’re expecting a list to be purely alphabetical from a reader’s perspective. The sorting doesn’t convert anything or ignore the case. It looks directly at the Unicode number of each character and sorts from there.
🌐
Baeldung
baeldung.com › home › java › core java › compare characters in java
Compare Characters in Java | Baeldung
December 26, 2024 - As shown above, the compare(char a, char b) method returns an int value.
🌐
Reddit
reddit.com › r/c_programming › how to compare characters in two strings to sort them alphabetically? (without c string library functions)
How to compare characters in two strings to sort them alphabetically? (without c string library functions) : r/C_Programming
February 10, 2015 - Is alphabetically supposed to mean the same thing? Judging by your code, you make instant decision if the lengths of the strings differ. This is inconsistent with lexicographical comparison. In lexicographical comparison lengths of the strings are secondary. In any case, what were you trying to do (even in Java) with this comparison
🌐
Oracle
docs.oracle.com › javase › tutorial › i18n › text › collationintro.html
Comparing Strings (The Java™ Tutorials > Internationalization > Working with Text)
For example, a report generator performs string comparisons when sorting a list of strings in alphabetical order. If your application audience is limited to people who speak English, you can probably perform string comparisons with the String.compareTo method. The String.compareTo method performs a binary comparison of the Unicode characters within the two strings.
🌐
Blogger
javahungry.blogspot.com › 2020 › 04 › compare-characters-java.html
How to Compare Characters in Java | Java Hungry
public class JavaHungry { public static void main(String args[]) { Character ch1 = new Character('X'); Character ch2 = new Character('Y'); Character ch3 = new Character('X'); System.out.println(ch1.equals(ch2)); System.out.println(ch2.equals(ch3)); System.out.println(ch1.equals(ch3)); } } Output: false false true That's all for today, please mention in comments in case you have any questions related to how to compare characters in java. About The Author · Subham Mittal has worked in Oracle for 3 years. Enjoyed this post? Never miss out on future posts by subscribing JavaHungry · Best Laptops for Programming · Best Books for Learning Java · Amazon Interview Question : First Non repeated character in String · Count total number of times each alphabet appears in the string java program code with example ·
🌐
Sentry
sentry.io › sentry answers › java › how to compare strings in java
How to compare strings in Java | Sentry
public class Main { public static ... than another string (that is, whether it comes before or after alphabetically), use String.compareTo()....
🌐
Programiz
programiz.com › java-programming › examples › alphabet
Java Program to Check Whether a Character is Alphabet or Not
Become a certified Java programmer. Try Programiz PRO! ... public class Alphabet { public static void main(String[] args) { char c = '*'; if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) System.out.println(c + " is an alphabet."); else System.out.println(c + " is not an alphabet."); } }
🌐
Runestone Academy
runestone.academy › ns › books › published › javajavajava › comparing-strings.html
7.9 Comparing Strings
In that case, s1 precedes s2 if s1’s kth character precedes s2’s. If the loop terminates normally, that means that all the characters compared were equal. In that case, the shorter string precedes the longer. For example, if the two strings were “alpha” and “alphabet,” then the method would return true, because “alpha” is shorter than “alphabet.”
🌐
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 ...
🌐
Quora
quora.com › How-do-you-compare-two-strings-or-character-arrays-letter-by-letter-in-Java
How to compare two strings or character arrays, letter-by-letter in Java - Quora
So if two string contains same letters, in same order and in same case they will be equals by equals() method. equals() method is defined in Object class and String class overrides...