Sort User-Defined Objects in Descending Order in Java

java.uti.Collections Examples

Java provides in build utility method sort() and Comparator Collections.reverseOrder() to sort the objects in descending order.
Collections.sort(persons, Collections.reverseOrder());
This is example demonstrates that we create a list of person objects and we compare each person's age by using compateTo() method of Comparable Interface. In short, we are sorting persons by age in descending order.
public class Person implements Comparable < Person > {

    private int id;

    private String name;

    private int age;

    public Person(int id, String name, int age) { = id; = name;
        this.age = age;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    public String toString() {

    public int compareTo(Person otherPerson) {
        return (this.getAge() - otherPerson.getAge());
Note that Person class implements Comparable interface and provides compareTo() method implementation.

Here is code to test and sort person objects:
import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

public class PersonSorterInASC {
    public static void main(String[] args) {


    private static void sortUserDefinedObjectsInAsc() {
        List < Person > persons = new ArrayList < Person > ();
        Person person1 = new Person(59, "John", 40);
        Person person12 = new Person(67, "Roger", 25);
        Person person13 = new Person(45, "Steven", 30);

        System.out.println("Before Sorting : " + persons);
        Collections.sort(persons, Collections.reverseOrder());
        System.out.println("After Sorting : " + persons);
Before Sorting : [John, Roger, Steven]
Sort in decending order : [John, Steven, Roger]

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course