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
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
The compareTo() method compares two strings lexicographically.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
October 20, 2025 - This interface is a member of the Java Collections Framework. Since: 1.2 · See Also: Comparator · int compareTo(T o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the ...
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
comparable - How to implement compareTo method in Java and what does it mean - Stack Overflow
I'm very new to java. I know that the Java Comparable interface has only one method, which has an int return type. However, I'm not sure what I am actually doing when I want to override the compareTo More on stackoverflow.com
🌐 stackoverflow.com
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
Can somebody help me with java (compareto()???)
A few thoughts: compareTo does not return -1, 0, 1. compareTo only guarantees that it returns either a negative, zero, or positive number. It would be incorrect to write code that compares the output of a call to compareTo against -1 or 1. e.g. if (a.compareTo(b) == -1) // wrong if (a.compareTo(b) < 0) // right CompareTo is used to establish the idea that one object can be greater than, equal to, or less than another object. For instance, Integers are Comparable, and they have a compareTo method. One Integer is larger than another if the int value it contains is larger (numerically) than the other. Strings are Comparable. Doubles are Comparable. As you noted, many other things can be Comparable, such as Students. Java chose to compare Strings lexicographically. This is similar to how a real-life dictionary would sort words in the English alphabet. Essentially, Strings are sorted the same way they would be sorted in the dictionary, and we use the ASCII table as the alphabet (instead of a regular 26-letter alphabet). This implies that capital letters come before lower-case letters. (e.g. "Z" < "a") The ternary operator is ? :. The syntax is: b ? x : y Where b is a boolean expression. x and y are expressions. If b is true, then the ternary operator returns x. Otherwise, it returns y. For instance, rather than writing: int x = 3; // or whatever other value you want if (x < 5) System.out.println("x is less than 5"); else System.out.println("x is greater than or equal to 5"); You can instead write: int x = 3; // or whatever other value you want System.out.println(x < 5 ? "x is less than 5" : "x is greater than or equal to 5"); More on reddit.com
🌐 r/learnprogramming
5
6
January 20, 2013
🌐
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

🌐
Stack Overflow
stackoverflow.com › questions › 72160928 › how-to-implement-compareto-method-in-java-and-what-does-it-mean
comparable - How to implement compareTo method in Java and what does it mean - Stack Overflow
This is where writing a custom (@Override) compareTo method comes in. By default the compareTo method returns either 1, 0 or -1. There is no inherent meaning to these numbers, in fact they can be any numbers you like (as long as they are different).
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
🌐
Reddit
reddit.com › r/learnprogramming › can somebody help me with java (compareto()???)
r/learnprogramming on Reddit: Can somebody help me with java (compareto()???)
January 20, 2013 -
  • I have a vague idea of what it does. It returns -1,0,1 depending on the circumstances.

  • But this is what I don't get. If you're comparing strings, how does it behave? I'm not even sure how the code looks like.. It doesn't make sense to compare strings.

  • How does the ordering work for this function if you're comparing a string? Is it the length of the string?

  • I just don't understand it.

  • Also taking a look at the following website and scrolling down:

http://javarevisited.blogspot.ca/2011/11/how-to-override-compareto-method-in.html

  • we see a "Student" class. Specifically paying attention to :

  • "return (this.id < otherStudent.id ) ? -1: (this.id > otherStudent.id) ? 1:0"

  • What are the following strings of symbols trying to say? I don't understand any of it =/. Can somebody break it down? Where did the ? -1: come from and what does it mean?

  • Same with the ? 1:0

  • Also if we look at the same page, you'll see another class below the "Student" class.

  • This time, it's the same student class but implements comparable<student>. Why does the compareTo() Method return the following value? "return age - otherStudent.age;"

Top answer
1 of 3
4
A few thoughts: compareTo does not return -1, 0, 1. compareTo only guarantees that it returns either a negative, zero, or positive number. It would be incorrect to write code that compares the output of a call to compareTo against -1 or 1. e.g. if (a.compareTo(b) == -1) // wrong if (a.compareTo(b) < 0) // right CompareTo is used to establish the idea that one object can be greater than, equal to, or less than another object. For instance, Integers are Comparable, and they have a compareTo method. One Integer is larger than another if the int value it contains is larger (numerically) than the other. Strings are Comparable. Doubles are Comparable. As you noted, many other things can be Comparable, such as Students. Java chose to compare Strings lexicographically. This is similar to how a real-life dictionary would sort words in the English alphabet. Essentially, Strings are sorted the same way they would be sorted in the dictionary, and we use the ASCII table as the alphabet (instead of a regular 26-letter alphabet). This implies that capital letters come before lower-case letters. (e.g. "Z" < "a") The ternary operator is ? :. The syntax is: b ? x : y Where b is a boolean expression. x and y are expressions. If b is true, then the ternary operator returns x. Otherwise, it returns y. For instance, rather than writing: int x = 3; // or whatever other value you want if (x < 5) System.out.println("x is less than 5"); else System.out.println("x is greater than or equal to 5"); You can instead write: int x = 3; // or whatever other value you want System.out.println(x < 5 ? "x is less than 5" : "x is greater than or equal to 5");
2 of 3
2
Thank you lightcloud5 and TinyLebowski. This clears up most mysteries I'm having. I still don't quite understand how instances of classes can be compared though. Is this how it works? We Override the compareTo() method and write our own. Then we specifically tell how to compare using what we think is appropriate. So in the end, we can compare with either int, double, float, string, etc. We just have to define how to compare things right?
🌐
Zero To Mastery
zerotomastery.io › blog › java-compareto-method
Beginner's Guide To compareto In Java (With Code Examples) | Zero To Mastery
March 3, 2025 - It’s Java’s built-in method for comparing objects, helping you sort lists, rank values, and organize data the way you want. But if you’ve never used it before, it might seem a little weird. Why does it return -1, 0, or 1 instead of true or false? Don’t worry because we’ll cover all that and more, so that by the end of this guide, you’ll not only understand how compareTo works - you’ll be confident using it to compare strings, numbers, and even custom objects.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string compareto() method
Java String CompareTo() Method
December 25, 2024 - The java string class compareTo() method returns the 0 value if both strings are lexicographically equal. If the compared string is greater lexicographically then the positive value is returned otherwise the negative value is returned.
🌐
Medium
medium.com › @AlexanderObregon › javas-compareto-and-clone-explained-32acb444e6b2
Understanding compareTo() and clone() in Java | Medium
July 10, 2024 - The compareTo() method is a vital part of the Comparable interface in Java, designed to allow objects of a class to be compared to each other. This is especially useful for sorting collections of objects.
🌐
Codecademy
codecademy.com › docs › java › strings › .compareto()
Java | Strings | .compareTo() | Codecademy
July 14, 2025 - The .compareTo() method is a built-in Java method compares two strings lexicographically by evaluating the Unicode value of each character.
🌐
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.
🌐
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.
🌐
Programiz
programiz.com › java-programming › library › string › compareto
Java String compareTo()
Become a certified Java programmer. Try Programiz PRO! ... The compareTo() method compares two strings lexicographically (in the dictionary order).
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.compareto
String.CompareTo(String) Method (Java.Lang) | Microsoft Learn
</blockquote> If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value: <blockquote> this.length()-anotherString.length() </blockquote> For finer-grained String comparison, refer to java.text.Collator.
🌐
Scaler
scaler.com › home › topics › java string compareto() method
Java String compareTo() Method with Examples - Scaler Topics
May 10, 2023 - Thus, using the compareTo() in Java we can find the length of the string by comparing with an empty string as it returns the length of the non-empty string with positive and negative signs as per the position of the non-empty string.
🌐
Jenkov
jenkov.com › tutorials › java-collections › comparable.html
Java Comparable
October 4, 2020 - The Java Comparable interface is used to make your classes comparable, so they can be automatically sorted using Java's built-in sorting functionality.
🌐
Quora
quora.com › How-does-one-effectively-use-the-compareTo-method-in-java-for-checking-string-value
How does one effectively use the compareTo() method in java for checking string value? - Quora
Answer (1 of 7): I will answer this question based on my understanding of what you are asking.... and I am giving u a general method to do so The general method to compare is as follows: public String compare(ArrayList people, String name) { String result; result =...
🌐
FavTutor
favtutor.com › blogs › compareto-method-java
compareTo() method in Java
October 25, 2023 - The compareTo() method is a built-in method in the Java String class that allows us to compare two strings lexicographically. It is based on the Unicode values of the characters in the strings.