String.compareTo might or might not be what you need.

Take a look at this link if you need localized ordering of strings.

Answer from Buhb on Stack Overflow
🌐
Mkyong
mkyong.com › home › java › java string compareto() examples
Java String compareTo() examples - Mkyong.com
January 24, 2022 - The "a" is lexicographically precedes the argument string "c"; it returns a negative integer. In simple words, for lexicographical or alphabetical order, "a" comes before the "c" and returns a negative integer.
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).
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
[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
October 27, 2011
Tips for printing two Strings in alphabetical order?
the first test is right This is not right. Sometimes you might have the wrong code/logic, but still get the right answer. I think this is a good case in point, of why you need more than just one test (with different words). In your if statement you compare secondString != firstString. All this does is compare if the two String variables point to the same object. You aren't doing any sort of alphabetic comparison. In fact since the two strings are never pointing to the same object, this will always mean they are not equal to each other and you enter the if always. As almost always with such a common function like alphabetic comparison, look at the [Java docs]( https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String) ) for the answer More on reddit.com
🌐 r/javahelp
14
2
October 23, 2017
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
July 22, 2014
🌐
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 ...
🌐
Medium
medium.com › @AlexanderObregon › sorting-text-alphabetically-with-java-code-177cfc072947
Sorting Text Alphabetically with Java Code | Medium
July 26, 2025 - Both of these methods follow the same comparison logic, but they work on different data structures. Arrays get passed directly to Arrays.sort, while lists go through Collections.sort. In the background, both will use the same sorting algorithm if the data qualifies. The important part is how the characters get matched up and how the code decides what comes first. When you sort an array of strings in Java, the comparisons happen with String.compareTo.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › compareTo
Java String compareTo() - Compare Strings Alphabetically | Vultr Docs
May 15, 2025 - The compareTo() method in Java is a crucial function from the String class that facilitates the alphabetical comparison of two strings. This method is commonly used in Java string comparison, especially for sorting strings, determining ...
Find elsewhere
🌐
Reddit
reddit.com › r/homeworkhelp › [intro comp. sci.] sorting an arraylist of strings alphabetically in java?
r/HomeworkHelp on Reddit: [Intro Comp. Sci.] Sorting an ArrayList of Strings Alphabetically in Java?
October 27, 2011 -

I'm working with an ArrayList of Animal objects, each with a String name that can be entered and added. I want them ordered alphabetically, but I'm not sure how to do this.

I know to override the CompareTo method after implementing comparable and to use the Collections.sort method, but then what, and how?

My idea was to somehow implement an ArrayList of characters and use charAt to find the compare two Strings with each other, but I can't really figure out if this is the best way, or if there is a better way.

Surely, I cannot be the first person to alphabetize a list of Strings in an array, so how is this typically done? Thanks!

I should add, I know to use CompareTo and Collections already. What I don't know is which algorithm to implement in CompareTo that will actually compare two Strings alphabetically.

🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-lexicographically-in-java
Compare two strings lexicographically in Java - GeeksforGeeks
July 11, 2024 - Each character of both the strings is converted into a Unicode value for comparison. ... // Java program to show how to compare Strings // using library function public class Test { public static void main(String[] args) { String s1 = "Ram"; String s2 = "Ram"; String s3 = "Shyam"; String s4 = "ABC"; System.out.println(" Comparing strings with compareTo:"); System.out.println(s1.compareTo(s2)); System.out.println(s1.compareTo(s3)); System.out.println(s1.compareTo(s4)); } }
🌐
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)
November 10, 2018 - If you want to know which of two strings is larger, you can use String.compareTo(). ... Okay, thank you so much! ... If you go through the Java™ Tutorials, you will find lots aout sorting. You might also find that there is a Comparator already provided in the String class for sorting a before B.
🌐
Medium
medium.com › javarevisited › understanding-javas-operator-equals-method-and-compareto-method-through-real-life-9a636e925e4f
Understanding Java's == Operator, equals() Method, and ...
May 13, 2025 - In Java, the compareTo() method helps to compare objects based on their order, like alphabetically sorting names or checking which date comes first.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-names-in-an-alphabetical-order
Java Program to Sort Names in an Alphabetical Order - GeeksforGeeks
July 23, 2025 - The idea is to compare the strings on the basis of their unicode and swap them in accordance with the returned int value based on the comparison between the two strings using compareTo() method.
🌐
W3Docs
w3docs.com › java
Comparing strings by their alphabetical order
String str1 = "apple"; String str2 = "banana"; int result = str1.compareTo(str2); if (result < 0) { System.out.println(str1 + " comes before " + str2); } else if (result > 0) { System.out.println(str1 + " comes after " + str2); } else { System.out.println(str1 + " is equal to " + str2); } ... In this example, the output will be "apple comes before banana", because "a" comes before "b" in the alphabet.
🌐
DEV Community
dev.to › tpointtechblog › java-string-comparison-smackdown-compareto-in-action-24n2
Java String Comparison Smackdown: compareTo( ) in Action! - DEV Community
May 5, 2025 - Imagine you’re comparing two words: If both words are exactly the same, compareTo() returns 0. If the first word comes before the second in alphabetical order, it returns a negative number.
🌐
Quora
quora.com › How-do-you-lexicographically-compare-strings-in-Java
How to lexicographically compare strings in Java - Quora
Answer (1 of 3): Let me define it simply > Lexicographically String means strings must be sorted according to Alphabetically (default natural sorting order). Comparing String in default natural sorting order and customized sorting order can be done through by implementing comparable interface a...
🌐
Quora
quora.com › How-do-you-compare-strings-alphabetically-in-Java
How to compare strings alphabetically in Java - Quora
Answer (1 of 3): “German phone book, case‑insensitive, accent‑insensitive, numeric”, using standard Java SE 1.1 java.text.* APIs. [code]import java.text.Collator; import java.text.CollationKey; import java.util.Locale; public class GermanPhonebook { private static final Collator COLLATOR; ...
🌐
Reddit
reddit.com › r/javahelp › tips for printing two strings in alphabetical order?
r/javahelp on Reddit: Tips for printing two Strings in alphabetical order?
October 23, 2017 -

I'm taking a CS 1 course and we're using Zybooks for some java homework, and I'm stuck on one of the problems. The problem states:

Print the two strings in alphabetical order. Assume the strings are lowercase. End with newline. Sample output:

capes rabbits

I have this so far, and the first test is right, but the second one is wrong. I want to be able to successfully complete all of the "tests"

My code so far:

import java.util.Scanner;

public class OrderStrings {
   public static void main (String [] args) {
      String firstString;
      String secondString;

      firstString  = "rabbits";
      secondString = "capes";
      
    if(secondString != firstString) {
       System.out.println(secondString+" "+firstString);
    }
    else{
       System.out.println(firstString+" "+secondString);
    }
     

   }
}

The first test outputs this

capes rabbits

the output that is wrong displays this for some reason:

your input: rabbits capes

expected output: capes rabbits

why does the second output switch around the Strings?

Everything before the if statement was already made for me by Zybooks.

🌐
TutorialsPoint
tutorialspoint.com › how-to-sort-a-string-in-java-alphabetically-in-java
How to Sort a String in Java alphabetically in Java?
April 22, 2025 - Convert the given string to a character array using the toCharArray() method. Compare the first two elements of the array. If the first element is greater than the second, swap them. Then, compare the 2nd and 3rd elements if the second element is greater than the 3rd, swap them. Repeat this till the end of the array. ... import java.util.Arrays; import java.util.Scanner; public class SortingString { public static void main(String args[]) { int temp, size; String str = "Tutorialspoint"; char charArray[] = str.toCharArray(); size = charArray.length; for(int i = 0; i < size; i++ ) { for(int j = i+1; j < size; j++) { if(charArray[i]>charArray[j]) { temp = charArray[i]; charArray[i] = charArray[j]; charArray[j] = (char) temp; } } } System.out.println("Third largest element is: "+Arrays.toString(charArray)); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › sort-a-string-in-java-2-different-ways
Sort a String in Java (2 different ways) - GeeksforGeeks
Arrays.sort(char c[]) method sort characters based on their ASCII value, we can define our custom Comparator to sort a string.
Published   March 18, 2024
🌐
Oracle
docs.oracle.com › javase › tutorial › i18n › text › collationintro.html
Comparing Strings (The Java™ Tutorials > Internationalization > Working with Text)
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. Applications that sort through text perform frequent string comparisons. For example, a report generator performs string comparisons when sorting a list of strings in alphabetical order.