Java Program to Sort LinkedList using Comparable

Last Updated : 20 Jul, 2026

In Java, LinkedList is a part of the collection framework provided in java.util package. LinkedList is a linear data structure in Java that stores elements as nodes linked together. When a LinkedList contains user-defined objects, the Comparable interface can be used to define their natural ordering, allowing the list to be sorted using Collections.sort().

  • Comparable defines the natural ordering of objects by implementing the compareTo() method.
  • Collections.sort() sorts the LinkedList according to the comparison logic defined in compareTo().
  • A LinkedList can be sorted in both ascending and descending order using different approaches.

Methods to Sort a LinkedList Using Comparable

Java provides the following approaches to sort a LinkedList using the Comparable interface.

1. Sort LinkedList in Ascending Order Using Comparable

The Comparable interface is implemented by the user-defined class, and the compareTo() method defines the natural ordering. Calling Collections.sort() arranges the elements in ascending order.

Syntax

Collections.sort(list);

Example to Sort LinkedList in Ascending Order

The following example demonstrates how the Comparable interface defines the natural ordering of Student objects. The compareTo() method compares the Rank field, and Collections.sort() arranges the LinkedList in ascending order.

Java
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

// User defined class implements Comparable
class Student implements Comparable<Student> {
    String Name;
    int Id;
    int Rank;

    Student(String name, int id, int rank)
    {
        this.Name = name;
        this.Id = id;
        this.Rank = rank;
    }

    // Override the compareTo() method
    @Override public int compareTo(Student s)
    {
        if (Rank > s.Rank) {
            return 1;
        }
        else if (Rank == s.Rank) {
            return 0;
        }
        else {
            return -1;
        }
    }
}

public class Main {
    // Main driver method
    public static void main(String[] args)
    {
        // Create one LinkedList for Student object
        LinkedList<Student> List
            = new LinkedList<Student>();
        List.add(new Student("Meet", 32, 2));
        List.add(new Student("Jhon", 11, 5));
        List.add(new Student("Sham", 92, 1));
        List.add(new Student("William", 86, 3));
        List.add(new Student("Harry", 35, 4));

        // Print the Unsorted LinkedList
        System.out.println("UnSorted List");
        for (Student s : List) {
            System.out.println(s.Rank + " " + s.Name + " "
                               + s.Id);
        }
        System.out.println();

        // sort in ascending order
        Collections.sort(List);

        // Print the sorted LinkedList
        System.out.println("Sorted List");
        for (Student s : List) {
            // Print the sorted LinkedList
            System.out.println(s.Rank + " " + s.Name + " "
                               + s.Id);
        }
    }
}

Output
UnSorted List
2 Meet 32
5 Jhon 11
1 Sham 92
3 William 86
4 Harry 35

Sorted List
1 Sham 92
2 Meet 32
3 William 86
4 Harry 35
5 Jhon 11

2. Sort LinkedList in Descending Order Using Collections.reverseOrder()

Instead of changing the comparison logic, Java provides Collections.reverseOrder() to reverse the natural ordering defined by Comparable.

A. Using Collections.reverseOrder()

In this approach, we have to first sort a LinkedList in Ascending order and after that using Collections.reverseOrder() we can reverse the order of sorted LinkedList.

Syntax:

Collections.sort(Listobject,Collections.reverseOrder())

Example Using Collections.reverseOrder()

The following example demonstrates how Collections.reverseOrder() reverses the natural ordering defined by Comparable, allowing the LinkedList to be sorted in descending order without modifying the compareTo() method.

Java
// Sort LinkedList using Comparable in Java
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

// User defined class implements Comparable
class Student implements Comparable<Student> {
    String Name;
    int Id;
    int Rank;

    Student(String name, int id, int rank)
    {
        this.Name = name;
        this.Id = id;
        this.Rank = rank;
    }

    // Override the compareTo() method
    @Override public int compareTo(Student s)
    {
        if (Rank > s.Rank) {
            return 1;
        }
        else if (Rank == s.Rank) {
            return 0;
        }
        else {
            return -1;
        }
    }
}

public class Main {
    // Main driver method
    public static void main(String[] args)
    {
        // Create one LinkedList for Student object
        LinkedList<Student> List
            = new LinkedList<Student>();
        List.add(new Student("Meet", 32, 2));
        List.add(new Student("Jhon", 11, 5));
        List.add(new Student("Sham", 92, 1));
        List.add(new Student("William", 86, 3));
        List.add(new Student("Harry", 35, 4));

        // Print the Unsorted LinkedList
        System.out.println("UnSorted List");
        for (Student s : List) {
            System.out.println(s.Rank + " " + s.Name + " "
                               + s.Id);
        }
        System.out.println();

        // sort in descending order
        Collections.sort(List, Collections.reverseOrder());

        // Print the sorted LinkedList
        System.out.println("Sorted List");
        for (Student s : List) {
            // Print the sorted LinkedList
            System.out.println(s.Rank + " " + s.Name + " "
                               + s.Id);
        }
    }
}

Output
UnSorted List
2 Meet 32
5 Jhon 11
1 Sham 92
3 William 86
4 Harry 35

Sorted List
5 Jhon 11
4 Harry 35
3 William 86
2 Meet 32
1 Sham 92

3. Sort LinkedList in Descending Order by Overriding compareTo()

Another approach is to modify the comparison logic inside the compareTo() method itself so that objects are compared in reverse order.

Java
// Sort LinkedList using Comparable in Java
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

// User defined class implements Comparable
class Student implements Comparable<Student> {
    String Name;
    int Id;
    int Rank;

    Student(String name, int id, int rank)
    {
        this.Name = name;
        this.Id = id;
        this.Rank = rank;
    }

    // Override the compareTo() method
    @Override public int compareTo(Student s)
    {
        // Changed the Comparison logic
        if (Rank < s.Rank) {
            return 1;
        }
        else if (Rank == s.Rank) {
            return 0;
        }
        else {
            return -1;
        }
    }
}

public class Main {
    // Main driver method
    public static void main(String[] args)
    {
        // Create one LinkedList for Student object
        LinkedList<Student> List
            = new LinkedList<Student>();
        List.add(new Student("Meet", 32, 2));
        List.add(new Student("Jhon", 11, 5));
        List.add(new Student("Sham", 92, 1));
        List.add(new Student("William", 86, 3));
        List.add(new Student("Harry", 35, 4));

        // Print the Unsorted LinkedList
        System.out.println("UnSorted List");
        for (Student s : List) {
            System.out.println(s.Rank + " " + s.Name + " "
                               + s.Id);
        }
        System.out.println();

        // sort in ascending order
        Collections.sort(List);

        // Print the sorted LinkedList
        System.out.println("Sorted List");
        for (Student s : List) {
            // Print the sorted LinkedList
            System.out.println(s.Rank + " " + s.Name + " "
                               + s.Id);
        }
    }
}

Output
UnSorted List
2 Meet 32
5 Jhon 11
1 Sham 92
3 William 86
4 Harry 35

Sorted List
5 Jhon 11
4 Harry 35
3 William 86
2 Meet 32
1 Sham 92
Comment