Compare Strings Alphabetically in Java

Rupam Yadav May 31, 2021
  1. Compare Strings Alphabetically Using compareTo()
  2. Compare Strings Alphabetically Using the Traditional Way
Compare Strings Alphabetically in Java

There are several ways to compare two or more strings in Java, but if you want to compare the strings lexicographically (alphabetically), here’s the article for you. The lexicographical order follows the word arrangement in a dictionary. Below, we have examples that show two ways of comparing strings alphabetically in Java.

Compare Strings Alphabetically Using compareTo()

In the example, we compare several strings to see if the results are correct. The compareTo() method comes with the String class, and thus we can call it with any string to compare it with another string. Below we compare s1 with s2, s3 with s4, s5 with s6, and so on.

When we compare the strings using compareTo(), this method returns an int value that tells us where strings should come before or after or if they’re equal. For example, if we compare s1 that has the value apple with s2 that has orange using s1.compare(s2), the comparedResult function will get a negative integer - this means that the s1 value comes before s2.

If comparedResult gets a positive integer, as when s3 is compared with s4, it means that s3 comes after s4 because lexicographically, capital letters come before the small letters.

If the compareTo() method returns zero, then it means that both the compared strings are equal, as in the case of s9 and s10.

class CompareStrings {
    public static void main(String args[]) {
        String s1 = "apple";
        String s2 = "orange";
        compareStrings(s1, s2);
        String s3 = "apple";
        String s4 = "Orange";
        compareStrings(s3, s4);
        String s5 = "sole";
        String s6 = "soul";
        compareStrings(s5, s6);
        String s7 = "john";
        String s8 = "johnson";
        compareStrings(s7, s8);
        String s9 = "one";
        String s10 = "one";
        compareStrings(s9, s10);

    }

    public static void compareStrings(String s1, String s2) {

        int comparedResult = s1.compareTo(s2);

        if (comparedResult > 0) {
            System.out.println(s1 + " comes after " + s2);
        } else if (comparedResult < 0) {
            System.out.println(s1 + " comes before " + s2);
        } else {
            System.out.println(s1 + " is equal to " + s2);
        }


    }
}

Output:

apple comes before orange
apple comes after Orange
sole comes before soul
john comes before johnson
one is equal to one

Compare Strings Alphabetically Using the Traditional Way

In this example, we take the same strings with the same output as the previous example, but the method is different. Instead of using a method from any class, we create our own methods. The compareStrings() is the method where the comparison occurs.

In compareStrings(), we create a loop that checks until the end of both the strings, s1 and s2. Inside the loop, we first get the characters of the string using charAt() and cast it to int, which returns an ASCII value. We do this for both the strings and then compare the ASCII values. If all the ASCII values are equal, that means that both strings are equal as well.

If the ASCII values are different, then we return the difference between the ASCII values of strings using (int) s1.charAt(i) - (int) s2.charAt(i);. After the loop, we check the length of the strings and then return the difference between them.

At last, take the int value returned by the compareStrings() method and pass it with the strings to the getComparisonResult() function, which prints the result whether the string should come before or after, or whether they are equal.

class CompareStrings {
    public static void main(String[] args) {
        
        String s1 = "apple";
        String s2 = "orange";
        int getValue1 = compareStrings(s1, s2);
        
        String s3 = "apple";
        String s4 = "Orange";
        int getValue2 = compareStrings(s3, s4);
        
        String s5 = "sole";
        String s6 = "soul";
        int getValue3 = compareStrings(s5, s6);
        
        String s7 = "john";
        String s8 = "johnson";
        int getValue4 = compareStrings(s7, s8);
        
        String s9 = "one";
        String s10 = "one";
        int getValue5 = compareStrings(s9, s10);

        getComparisonResult(getValue1, s1, s2);
        getComparisonResult(getValue2, s3, s4);
        getComparisonResult(getValue3, s5, s6);
        getComparisonResult(getValue4, s7, s8);
        getComparisonResult(getValue5, s9, s10);
        
    }

    public static int compareStrings(String s1, String s2) {

        for (int i = 0; i < s1.length() && i < s2.length(); i++) {
            if ((int) s1.charAt(i) == (int) s2.charAt(i)) {
                continue;
            } else {
                return (int) s1.charAt(i) - (int) s2.charAt(i);
            }
        }

        if (s1.length() < s2.length()) {
            return (s1.length() - s2.length());
        } else if (s1.length() > s2.length()) {
            return (s1.length() - s2.length());
        } else {
            return 0;
        }
    }

    private static void getComparisonResult(int value, String s1, String s2) {
        if (value > 0) {
            System.out.println(s1 + " comes after " + s2);
        } else if (value < 0) {
            System.out.println(s1 + " comes before " + s2);
        } else {
            System.out.println(s1 + " and " + s2 + " are equal");
        }
    }
}

Output:

apple comes before orange
apple comes after Orange
sole comes before soul
john comes before johnson
one and one are equal
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java String