You just have to define that Animal implements Comparable<Animal> i.e. public class Animal implements Comparable<Animal>. And then you have to implement the compareTo(Animal other) method that way you like it.

@Override
public int compareTo(Animal other) {
    return Integer.compare(this.year_discovered, other.year_discovered);
}

Using this implementation of compareTo, animals with a higher year_discovered will get ordered higher. I hope you get the idea of Comparable and compareTo with this example.

Answer from user1983983 on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
October 20, 2025 - {(x, y) such that x.compareTo(y) == 0}. It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method: ... This interface is a member of the Java Collections Framework.
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-interface-in-java-with-examples
Java Comparable Interface - GeeksforGeeks
Key Method: int compareTo(T obj) - Compares the current object with the specified object. ... import java.util.*; class Student implements Comparable<Student> { String name; int marks; Student(String name, int marks) { this.name = name; this.marks ...
Published   1 month ago
Discussions

I need some help understanding Comparable interface
By implementing Comparable, it's guaranteed that the object will have a compareTo() method. Then you can pass a list of that object to methods like Collections.sort(). If you look at the documentation for sort (link below) you can see that it says "all elements in the list must implement the Comparable interface". I think this link explains it well: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ Docs for sort(): https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List) More on reddit.com
🌐 r/learnjava
7
7
October 18, 2017
java - adding comparable interface and adding a compareTo() method - Stack Overflow
I'm having a bit of trouble adding the two noted above. I have two classes. Employee and Company. The employee class holds some information about the employees, instance variables including their ... More on stackoverflow.com
🌐 stackoverflow.com
Trying to understand the point of Comparable (if I even understand...)
The point is that if you implement Comparable for your class Pizza or Dog or Spaceship in some way that makes sense to you, then any method that takes Comparable can compare Pizza or Dog or Spaceship objects, despite never having heard of them before. A sort method doesn't have to understand the first thing about how to compare klingonBirdOfPrey and redDwarf, it just calls the compareTo method and trusts that the person who implemented it has got it figured out. More on reddit.com
🌐 r/learnjava
4
3
September 15, 2021
Comparable interface and comparing String length in Java; CompSci student having difficulty, but almost there
You are confused. First, Comparable is part of the standard java libraries. You don't need to define it yourself. You shouldn't define it yourself. Second, where did you get the idea that Compare3 implements Comparable? Third, you were asked to write a function that takes three Comparables and you wrote a function that takes three Strings. Other than that, the code is correct (yes, I'm serious here. I'm not trying to be funny. If you understand my comments you should be able to fix up the code in about 5 minutes). EDIT: abstract? Why did you make the class abstract? Your answer to #2 is missing the bit where you "prompt the user", but other than that it looks fine. More on reddit.com
🌐 r/learnprogramming
6
4
June 1, 2010
🌐
Reddit
reddit.com › r/learnjava › i need some help understanding comparable interface
r/learnjava on Reddit: I need some help understanding Comparable interface
October 18, 2017 -

I'm struggling to understand why we need to use compareTo for comparing things when I can just create a method that does the same thing. What I understand so far is that Interfaces can hold abstract methods without implementation, and once you implement that interface on a class you have to override the method from the interface and write the implementation. What I don't understand is what is significant about (implements Comparable<T>) if all I'm gonna do is override compareTo method so it returns 1 if larger, -1 if smaller or 0 if equal.

🌐
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
For example, if you have a list of cars you might want to sort them by year, the rule could be that cars with an earlier year go first. The Comparator and Comparable interfaces allow you to specify what rule is used to sort objects.
🌐
Codecademy
codecademy.com › docs › java › comparable
Java | Comparable | Codecademy
April 29, 2025 - The Comparable interface in Java specifies the natural ordering for objects of a custom class. It is part of the java.lang package and provides a mechanism for comparing objects of the same type.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - As the name suggests, Comparable is an interface defining a strategy of comparing an object with other objects of the same type.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 11 & JDK 11 )
January 20, 2026 - {(x, y) such that x.compareTo(y) == 0}. It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method: ... This interface is a member of the Java Collections Framework.
🌐
Medium
medium.com › @toimrank › java-comparable-7a72c13c3a96
Java Comparable. Comparable interface belongs to… | by Imran Khan | Medium
October 10, 2022 - Java Comparable Comparable interface belongs to java.lang package. Comparable interface allows us to sort arrays and collections. For custom sorting Comparable interface needs to implement and …
🌐
Medium
medium.com › @pratik.941 › understanding-comparable-and-comparator-interface-in-java-their-role-in-sorting-4338b3017fa9
Understanding Comparable and Comparator interface in Java: Their Role in Sorting | by Pratik T | Medium
October 4, 2024 - The compare() method compares two objects and returns: — A negative integer if the first argument is less than the second. — Zero if the first argument is equal to the second.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › OOBasics › ooComparable.html
11.14. The Comparable Interface — AP CSA Java Review - Obsolete
The Comparable interface just specifies the int compareTo(T o) method which will return a negative number if the current object is less than the passed one, 0 if they are equal, and a positive number if the current object is greater than the passed one. How do you compare two objects of any class?
🌐
Oracle
docs.oracle.com › en › java › javase › 18 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 18 & JDK 18)
August 18, 2022 - In other words, when a class's natural ordering is consistent with equals, the equivalence classes defined by the equivalence relation of the equals method and the equivalence classes defined by the quotient of the compareTo method are the same. This interface is a member of the Java Collections Framework.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 6)
{(x, y) such that x.compareTo(y) == 0}. It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method: {(x, y) such that x.equals(y)}. This interface is a member of the Java Collections Framework.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java comparable interface
Java Comparable Interface
July 1, 2022 - Java Comparable interface used to sort an array or list of objects by their natural order. Natural ordering of elements is imposed by compareTo() method.
🌐
Coding Shuttle
codingshuttle.com › java-programming-handbook › comparable-interface-in-java
Comparable Interface in Java | Coding Shuttle
April 9, 2025 - This blog explains the Comparable interface in Java with simple examples, showcasing how to sort custom objects using natural ordering. Learn how to implement compareTo(), handle different sorting scenarios like ascending, descending, and sorting by name, and understand when to use Comparable vs Comparator.
🌐
Medium
samedesilva.medium.com › java-comparable-interface-045b62790be5
Java comparable interface. Comparable is presents in Java.lang… | by Sameera De Silva | Medium
November 25, 2023 - Comparable is presents in Java.lang package Comparable interface is used to sort the objects with natural ordering. Comparable provides int CompareTo(T other ) method to sort elements.Here T means Type it can be Apple ,Cat any class.
🌐
Reddit
reddit.com › r/learnjava › trying to understand the point of comparable (if i even understand...)
r/learnjava on Reddit: Trying to understand the point of Comparable (if I even understand...)
September 15, 2021 -

What I understand is that it is an interface. An interface is a guarantee that we will implement the methods it provides somehow. The one method provided by Comparable is compareTo() that returns an int (like -1 if the second obj is greater, 0 if =, 1 if first obj is greater).

My questions is that was is the point of implements Comparable if we write our own version of the method to compare what we want anyways? Am I missing something about implementing and interfaces??

🌐
BeginnersBook
beginnersbook.com › 2017 › 08 › comparable-interface-in-java-with-example
Comparable Interface in Java with example
May 26, 2024 - Lists (and arrays) of objects that implement Comparable interface can be sorted automatically by Collections.sort (and Arrays.sort). Before we see how to sort an objects of custom objects, lets see how we can sort elements of arrays and Wrapper classes that already implements Comparable. import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String[] args) { /* * Integer class implements Comparable * Interface so we can use the sort method */ int[] arr = {11, 55, 22, 0, 89}; Arrays.sort(arr); System.
🌐
DigitalOcean
digitalocean.com › community › tutorials › comparable-and-comparator-in-java-example
Comparable and Comparator in Java Example | DigitalOcean
August 3, 2022 - Java provides Comparable interface which should be implemented by any custom class if we want to use Arrays or Collections sorting methods. The Comparable interface has compareTo(T obj) method which is used by sorting methods, you can check any Wrapper, String or Date class to confirm this.