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 )
1 week ago - This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-interface-in-java-with-examples
Java Comparable Interface - GeeksforGeeks
The Comparable interface in Java is used to define the natural ordering of objects of a class. It enables objects to be compared and sorted automatically without using an external Comparator.
Published   3 weeks ago
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 7 )
Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator. The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C.
🌐
Baeldung
baeldung.com › home › java › core java › guide to implementing the compareto method
Guide to Implementing the compareTo Method | Baeldung
May 29, 2025 - TreeMap and TreeSet are two implementations from the Java Collections Framework that assist us with the automatic sorting of their elements. We may use objects that implement the Comparable interface in a sorted map or as elements in a sorted set. Let’s look at an example of a custom class that compares players based on the number of goals they have scored: @Override public int compareTo(FootballPlayer anotherPlayer) { return Integer.compare(this.goalsScored, anotherPlayer.goalsScored); }
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 17 & JDK 17)
January 20, 2026 - This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
🌐
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 - This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 21 & JDK 21)
January 20, 2026 - This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Find elsewhere
🌐
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 Comparable interface is used to define the natural ordering of objects of a class. It is found in the java.lang package and has a single method compareTo(), which must be implemented by any class that implements this interface.
🌐
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 23 & JDK 23 [build 1])
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
🌐
Jenkov
jenkov.com › tutorials › java-collections › comparable.html
Java Comparable
October 4, 2020 - The Java Comparable interface,java.lang.Comparable, represents an object which can be compared to other objects. For instance, numbers can be compared, strings can be compared using alphabetical comparison etc.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 6)
Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator. The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › OOBasics › ooComparable.html
11.14. The Comparable Interface — AP CSA Java Review - Obsolete
In Java, you can sort objects of ... 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...
🌐
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - In order to be able to sort, we must define our Player object as comparable by implementing the Comparable interface: public class Player implements Comparable<Player> { // same as before @Override public int compareTo(Player otherPlayer) { ...
🌐
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
The Comparable interface allows an object to specify its own sorting rule with a compareTo() method.
Top answer
1 of 3
3

Let's get right to the heart of the matter. Imagine we have a class K that we've defined as implementing Comparable<K>, and two references a and b to objects of class K.

To know if a is "less thanb`, we write

if (a.compareTo(b) < 0) {
     //  compareTo returned a negative number
     ,,,
}

To know if a is "equal to" b, we write

if (a.compareTo(b) == 0) {
    // compareTo returned zero
    ...
}

And to know if a is "greater than" b, we write

if (a.compareTo(b) > 0) {
    // compareTo returned a positive integer
    ...
}

Does that clear things up a bit?

2 of 3
1

Sorting list of Custom Objects in ascending and descending Order

Java provides two interfaces to sort objects using data members of the class:

Comparable and Comparator interfaces

Java Comparable interface

Java Comparable interface is used to order the objects of user-defined class. This interface is found in java.lang package and contains only one method named compareTo(Object). It provide single sorting sequence only i.e. you can sort the elements on based on single data member only. public int compareTo(Object obj): is used to compare the current object with the specified object. We can sort the elements of: String objects Wrapper class objects User-defined class objects

Java Comparator interface

Java Comparator interface is used to order the objects of user-defined class. Collections class provides static methods for sorting the elements of collection. Method of Collections class for sorting List elements public void sort(List list, Comparator c): is used to sort the elements of List by the given Comparator.

References: http://corejavapractical.blogspot.in/2017/08/algorithms-in-java.html

package com.mycompany.projectname.corejava;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;


public class AlgorithmsDemo {

         public static void main(String[] args) {
                 //sortingCustomObjectsByComparable();
                 sortingCustomObjectsByComparator();
         }


         private static void sortingCustomObjectsByComparable(){

                 // Sort Projects by project id in ascending order.

                 List projects = new ArrayList<>();
                 Project project = new Project();
                 project.setProjectId(100);
                 project.setProjectName("project 100");
                 projects.add(project);

                 Project project2 = new Project();
                 project2.setProjectId(200);
                 project2.setProjectName("project 200");
                 projects.add(project2);

                 Project project3 = new Project();
                 project3.setProjectId(50);
                 project3.setProjectName("project 50");
                 projects.add(project3);

                 Collections.sort(projects);

                 printList(projects);

         }

         private static void sortingCustomObjectsByComparator(){

                 // Sort Projects by project id in ascending order.

                 List projects = new ArrayList<>();
                 Project project = new Project();
                 project.setProjectId(100);
                 project.setProjectName("project 100");
                 projects.add(project);

                 Project project2 = new Project();
                 project2.setProjectId(200);
                 project2.setProjectName("project 200");
                 projects.add(project2);

                 Project project3 = new Project();
                 project3.setProjectId(50);
                 project3.setProjectName("project 50");
                 projects.add(project3);

                 // Sorting project by project id in ascending order in Java
                 Collections.sort(projects);
                 printList(projects);

                 // Sorting project by project id in descending order in Java
        Collections.sort(projects, Collections.reverseOrder());
        printList(projects);


     // Sorting project by project name in ascending order in Java
          Comparator comparator = new Comparator() {
               @Override
               public int compare(Project o1, Project o2) {
                   // TODO Auto-generated method stub
                   return o1.getProjectName().compareTo(o2.getProjectName());
               }
          };
           Collections.sort(projects, comparator);
           printList(projects);

         }

         private static void printList(List projects){
                 for(Project project : projects){
                          System.out.println(project.getProjectId());
                          System.out.println(project.getProjectName());
                 }
         }

}

class Project implements Comparable{
         private int projectId;
         private String projectName;
         public int getProjectId() {
                 return projectId;
         }
         public void setProjectId(int projectId) {
                 this.projectId = projectId;
         }
         public String getProjectName() {
                 return projectName;
         }
         public void setProjectName(String projectName) {
                 this.projectName = projectName;
         }
         @Override
         public int compareTo(Project o) {
                 // TODO Auto-generated method stub
                 return this.projectId - o.getProjectId();
         }
}
🌐
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.
🌐
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Implementing compareTo
Implementing compareTo The compareTo method is the sole member of the Comparable interface, and is not a member of Object. However, it's quite similar in nature to equals and hashCode.
🌐
Codecademy
codecademy.com › docs › java › comparable
Java | Comparable | Codecademy
April 29, 2025 - It is part of the java.lang package and provides a mechanism for comparing objects of the same type. By implementing this interface, a class indicates that its instances can be ordered or sorted.
🌐
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 - This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.