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
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-interface-in-java-with-examples
Java Comparable Interface - GeeksforGeeks
Output shows the students sorted by ID in ascending order. class MyClass implements Comparable<MyClass> { @Override public int compareTo(MyClass obj) { // comparison logic } } Returns negative if this < obj. Returns 0 if this == obj. Returns positive if this > obj. Key Method: int compareTo(T obj) - Compares the current object with the specified object...
Published   3 weeks ago
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
1 week ago - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Codecademy
codecademy.com › docs › java › comparable
Java | Comparable | Codecademy
April 29, 2025 - This example shows how to use the Comparable interface with Java Collections and how to access elements after they’ve been sorted: ... The Great Gatsby by F. Scott Fitzgerald ($12.99) ... The Great Gatsby by F. Scott Fitzgerald ($12.99) Cheapest book: Pride and Prejudice by Jane Austen ($7.99) Most expensive book: The Great Gatsby by F. Scott Fitzgerald ($12.99) ... In this example, a Book class is created that implements the Comparable interface with comparison based on book prices.
🌐
BeginnersBook
beginnersbook.com › 2017 › 08 › comparable-interface-in-java-with-example
Comparable Interface in Java with example
May 26, 2024 - I wanted to sort the author names by last name first and if the last name is same then by first name. If you want to sort by the last name only then first line inside compareTo() method is enough. ... public class Author implements Comparable<Author> { String firstName; String lastName; String ...
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java comparable interface
Java Comparable Interface
July 1, 2022 - Java Comparable interface is part of Collection Framework. Learn the purpose of Comparable interface and use it in different scenarios. 1. Comparable Interface 1.1. Why Implement Comparable? In Java, if we want to sort a List of elements then we can Collections.sort() method.
🌐
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 - In this example, Student objects will be sorted by their rollNumber in ascending order. When to Use Comparable: — When you want to define a default or natural order for objects of a class.
🌐
DZone
dzone.com › coding › languages › java comparable interface in five minutes
Java Comparable Interface in Five Minutes
April 6, 2017 - In this first version, we’re comparing objects by their weight. In our compareTo() method we write an if condition that says if the apple’s weight is less than the other apple, return a negative number, to keep it simple, we’ll say -1.
Find elsewhere
🌐
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 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Tutorialspoint
tutorialspoint.com › java › java-using-comparable.htm
Java - How to Use Comparable?
In this example, we're using Comparable interface to sort a custom object Dog based on comparison criterias. import java.util.ArrayList; import java.util.Collections; import java.util.List; class Dog implements Comparable<Dog> { private String ...
🌐
Jenkov
jenkov.com › tutorials › java-collections › comparable.html
Java Comparable
October 4, 2020 - This sorting functionality can be used to sort collections of Java objects. Please keep in mind that the Comparable interface is intended for comparison of objects of the same class. In other words, comparing apples to apples and oranges to oranges.
🌐
Coding Shuttle
codingshuttle.com › home › handbooks › 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.
🌐
W3Schools Blog
w3schools.blog › home › comparable interface in java
Comparable interface in java - w3schools.blog
September 3, 2014 - * @author w3schools */ public class Student implements Comparable{ //data members private String name; private String rollNo; private int age; //no-argument constructor public Student(){ } //argument cnstructor public Student(String name, String rollNo, int age){ this.name = name; this.rollNo = rollNo; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRollNo() { return rollNo; } public void setRollNo(String rollNo) { this.rollNo = rollNo; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //Method for sorting logic public int compareTo(Object obj){ Student student=(Student)obj; return (this.name).compareTo(student.name); } }
🌐
DigitalOcean
digitalocean.com › community › tutorials › comparable-and-comparator-in-java-example
Comparable and Comparator in Java: Examples & Guide | DigitalOcean
August 3, 2022 - Comparator interface compare(Object o1, Object o2) method need to be implemented that takes two Object argument, it should be implemented in such a way that it returns negative int if the first argument is less than the second one and returns zero if they are equal and positive int if the first ...
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 6)
For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Scaler
scaler.com › topics › comparable-interface-in-java
Comparable Interface in Java with Examples- Scaler Topics
September 20, 2023 - Given is an array of pairs that each has two fields of type integer and string. If two strings are identical, sort the array based on their numeric value and then in ascending Lexicographical order. ... The Java code defines a class CustomPair, implementing the Comparable interface to enable ...
🌐
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - We can build one simply by making use of the Comparator or Comparable interfaces. Let’s use an example of a football team, where we want to line up the players by their rankings.
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-vs-comparator-in-java
Java Comparable vs Comparator - GeeksforGeeks
August 18, 2025 - The main difference between Comparable ... sorting logic externally. In this example, we use the Comparable interface to sort Movies by their release year using compareTo() 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 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
BootcampToProd
bootcamptoprod.com › comparable-interface-in-java
Comparable Interface in Java: A Complete Beginner's Guide - BootcampToProd
November 19, 2023 - Learn how to sort objects using the Comparable interface in Java. Our beginner's guide provides step-by-step instructions and examples for implementing the