What you need to use is the compareTo() method of Strings.

return this.getName().compareTo(i.getName());

That should do what you want.

Usually when implementing the Comparable interface, you will just combine the results of using other Comparable members of the class.

Below is a pretty typical implementation of a compareTo() method:

class Car implements Comparable<Car> {
    int year;
    String make, model;
    public int compareTo(Car other) {
        if (!this.make.equalsIgnoreCase(other.make))
            return this.make.compareTo(other.make);
        if (!this.model.equalsIgnoreCase(other.model))
            return this.model.compareTo(other.model);
        return this.year - other.year;
    }
}
Answer from jjnguy on Stack Overflow
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.string.compareto
String.CompareTo Method (System) | Microsoft Learn
Compares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String.
Discussions

Understanding the compareTo() method
The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value. The result is positive if the first string is lexicographically greater than the second string else the result would be negative. More on reddit.com
๐ŸŒ r/javahelp
16
15
August 8, 2020
How to use the Comparable CompareTo on Strings in Java - Stack Overflow
What you need to use is the compareTo() method of Strings. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - String compareTo understanding - Stack Overflow
I have a quick question about comparing two Strings. these are my Strings: String s1 = "bc"; String s2 = "bb"; My understanding is that s1 More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - What does the String classes compareTo() method return - Stack Overflow
In the Java API on oracles website: "compareTo Returns: "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string arg... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 )
October 20, 2025 - Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by calling Character.toLowerCase(Character.toUpperCase(character)) on ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-string-compareto-method-with-examples
Java String compareTo() Method with Examples - GeeksforGeeks
January 20, 2025 - The Java compareTo() method compares the given string with the current string lexicographically. It returns a positive number, a negative number, or 0.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ understanding the compareto() method
r/javahelp on Reddit: Understanding the compareTo() method
August 8, 2020 -

From what I have been reading, the compareTo() method returns the difference of the Unicode numerical values of two Strings when they are compared with each other. For instance, the String "hello" when compared with the String "hello" returns an integer value of zero, since they both have exactly the same Unicode characters in them. Based on my understanding of this method, "hello" should return zero when compared to "olleh", because the two Strings have the exact same Unicode characters in them. Instead, though, I am getting integer value of 7 returned to the console. Can someone break this down a bit for me to help me understand it better? Thanks in advance. Here is my code:

String str1 = "hello";
String str2 = "olleh";
System.out.println(str1.compareTo(str2)); // 7

๐ŸŒ
Particle
docs.particle.io โ€บ reference โ€บ device-os โ€บ api โ€บ string-class โ€บ compareto
compareTo() - String class | Reference | Particle
Compares two Strings, testing whether one comes before or after the other, or whether they're equal. The strings are compared character by character, using the ASCII values of the characters.
Find elsewhere
๐ŸŒ
Flutter
api.flutter.dev โ€บ flutter โ€บ dart-core โ€บ String โ€บ compareTo.html
compareTo method - String class - dart:core library - Dart API
If the strings have exactly the same content, they are equivalent with regard to the ordering. Ordering does not check for Unicode equivalence. The comparison is case sensitive. var relation = 'Dart'.compareTo('Go'); print(relation); // < 0 relation = 'Go'.compareTo('Forward'); print(relation); ...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ java string compareto() method
Java String CompareTo() Method
December 25, 2024 - If the parameter is null, a NullPointerException is thrown. Always ensure that the parameter is not null. The compareTo() method compares strings based on the Unicode value of each character.
๐ŸŒ
Text Compare
text-compare.com
Text Compare! - Find differences between two text files
Text Compare! is an online diff tool that can find the difference between two text documents. Just paste and compare.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ string โ€บ compareto
Java String compareTo()
The compareTo() method compares two strings lexicographically (in the dictionary order).
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ java-string-compareto-method
Java String compareTo() Method with Examples - TechVidvan
March 7, 2024 - The compareTo() function of the Java String class lexicographically compares the inputted string with the currently displayed string.
๐ŸŒ
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 ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java string compareto() examples
Java String compareTo() examples
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. System.out.println("a".compareTo("b")); // -1 System.out.println("a".compareTo("c")); // -2 System.out.println("a".compareTo("d")); // -3 System.out.println("a".compareTo("e")); // -4 System.out.println("1".compareTo("2")); // -1 System.out.println("1".compareTo("3")); // -2 System.out.println("1".compareTo("4")); // -3 System.out.println("1".compareTo("5")); // -4
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.string.compare
String.Compare Method (System) | Microsoft Learn
Compares substrings of two specified String objects using the specified comparison options and culture-specific information to influence the comparison, and returns an integer that indicates the relationship of the two substrings to each other ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Comparable.html
Comparable (Java Platform SE 8 )
October 20, 2025 - It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java/lang โ€บ java string compareto method
Java String compareTo Method
September 1, 2008 - The Java String compareTo() method is used to compare two strings lexicographically. It returns an integer value, and the comparison is based on the Unicode value of each character in the strings.