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 - For example, if one adds two keys ... 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 ...
🌐
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 )
For example, if one adds two keys ... 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 ...
🌐
Codecademy
codecademy.com › docs › java › comparable
Java | Comparable | Codecademy
April 29, 2025 - This example demonstrates how to create a simple class that implements the Comparable interface to enable natural sorting of objects: import java.util.Arrays; // Define a Student class that implements Comparable · class Student implements Comparable<Student> { private String name; private int id; // Constructor ·
🌐
BeginnersBook
beginnersbook.com › 2017 › 08 › comparable-interface-in-java-with-example
Comparable Interface in Java with example
May 26, 2024 - Last Updated: May 26, 2024 by Chaitanya Singh | Filed Under: java · Comparable interface is mainly used to sort the arrays (or lists) of custom objects. Lists (and arrays) of objects that implement Comparable interface can be sorted automatically by Collections.sort (and Arrays.sort).
🌐
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. — A positive integer if the first argument is greater than the second. ... import java.util.Comparator; public class Student { private String name; private int rollNumber; public Student(String name, int rollNumber) { this.name = name; this.rollNumber = rollNumber; } public String getName() { return name; } public int getRollNumber() { return rollNumber; } @Override public String toString() { return "Student{name='
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › comparable-and-comparator-in-java-example
Comparable and Comparator in Java: Examples & Guide | DigitalOcean
August 3, 2022 - Here is the final classes we have explaining Comparable and Comparator in Java. package com.journaldev.sort; import java.util.Comparator; public class Employee implements Comparable<Employee> { private int id; private String name; private int age; private long salary; public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } public long getSalary() { return salary; } public Employee(int id, String name, int age, int salary) { this.id = id; this.name = name; this.age = age; this.salary = salary; } @Override public int compareTo(Employee emp) { //let's sort the employee based on an id in ascending order //returns a negative integer, zero, or a positive integer as this employee id //is less than, equal to, or greater than the specified object.
🌐
Tutorialspoint
tutorialspoint.com › java › java-using-comparable.htm
Java - How to Use Comparable?
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Dog implements Comparable<Dog> { private String name; private int age; Dog() { } Dog(String n, int a) { name = n; age = a; } public String getDogName() { return name; } public int getDogAge() { return age; } // Overriding the compareTo method public int compareTo(Dog d) { return (this.name).compareTo(d.name); } @Override public String toString() { return this.name + "," + this.age; } } public class ComparableDemo { public static void main(String args[]) { // Takes a list o Dog objects List<Dog> list = new Ar
🌐
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Implementing compareTo
Note that Boolean did not implement Comparable until version 1.5, however. ... import java.util.*; /** @since Java 7 */ public final class Account implements Comparable<Account> { enum AccountType {CASH, MARGIN, RRSP}; public Account ( String firstName, String lastName, int accountNumber, int balance, boolean isNewAccount, AccountType accountType ) { //..detailed parameter validations elided this.firstName = Objects.requireNonNull(firstName); this.lastName = Objects.requireNonNull(lastName); this.accountNumber = accountNumber; this.balance = balance; this.isNewAccount = isNewAccount; this.accountType = accountType; } /** * @param that is a non-null Account.
🌐
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 - One exception is java.math.BigDecimal, whose natural ordering equates BigDecimal objects with equal values and different precisions (such as 4.0 and 4.00). For the mathematically inclined, the relation that defines the natural ordering on a given class C is: {(x, y) such that x.compareTo(y) <= 0}. The quotient for this total order is:
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java comparable interface
Java Comparable Interface
July 1, 2022 - In this tutorial, we learned about Comparable interface. This interface helps in imposing a natural order on objects with simple interface implementation. We also learned to sort a list of strings, array of strings, list of integers, and array of integers. We learned how to sort Employee objects in Java using Comparable.
🌐
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... In the List Sorting Chapter, you learned how to sort lists alphabetically and numerically, but what if the list has objects in it? To sort objects you need to specify a rule that decides how objects should be sorted. 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.
🌐
Blogger
xyzcode.blogspot.com › 2017 › 02 › use-javautilcomparator-and.html
XYZ CODE: Use java.util.Comparator and java.lang.Comparable interfaces
OCPJP>cat Compare.java · import java.util.*; public class Compare { public static void main(String...args) { List<Person> persons = new ArrayList<>(); persons.add(new Person("aa", 20)); persons.add(new Person("cc", 10)); persons.add(new Person("bb", 30)); Set<Person> persons2 = new TreeSet<>(persons); System.out.println("TreeSet persons"); System.out.println(persons2); Set<Person> persons3 = new HashSet<>(persons); System.out.println("Hash persons"); System.out.println(persons3); System.out.println("ArrayList persons"); System.out.println(persons); System.out.println("ArrayList persons sorted
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-vs-comparator-in-java
Java Comparable vs Comparator - GeeksforGeeks
August 18, 2025 - The main difference between Comparable and Comparator is: Comparable: It is used to define the natural ordering of the objects within the class. Comparator: It is used to define custom sorting logic externally.
🌐
Team Treehouse
teamtreehouse.com › community › java-compareto-sorting-sorting-multiple-fields-for-same-object
Java CompareTo Sorting - Sorting Multiple Fields for Same Object (Example) | Treehouse Community
September 14, 2017 - Here is a good resource: https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/ import java.util.Comparator; ``` public static Comparator<Player> playerHeightComparator = new Comparator<Player>() { public int compare(Player p1, Player p2) { Integer playerHeight1 = p1.getHeightInInches(); Integer playerHeight2 = p2.getHeightInInches(); return playerHeight1.compareTo(playerHeight2); } }; public static Comparator<Player> playerLastNameComparator = new Comparator<Player>() { public int compare(Player p1, Player p2) { String playerName1 = p1.getLastName(); String playerName2 = p2.getLastName(); return playerName1.compareTo(playerName2); } }; `Collections.sort(height3540, Player.playerHeightComparator);` Posting to the forum is only allowed for members with active accounts.