Open In App

Java Program to Sort LinkedList using Comparable

Last Updated : 26 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, LinkedList is a part of the collection framework provided in java.util package. LinkedList is a linear data structure where all the elements are unsorted in contiguous memory locations. The advantage of LinkedList is it is dynamic and easy to insert and delete any element. We can not access any node of LinkedList directly we need to start with the Head node and traverse through a LinkedList.

Comparable provides a single sorting sequence. If we use Comparable then it will affect the original class. Comparable Interface provides compareTo() method to sort elements. In java, comparable is provided by the java.lang package. We can sort the LinkedList by invoking the Collections.sort(List) method. 

LinkedList can be sorted in the following order using Comparable:

  1. Ascending Order
  2. Descending Order
    • Using Collections.reverseOrder()
    • Override the CompareTo() method

Type 1: Ascending Order

To sort a LinkedList in Ascending order using Comparable we need to implement the Comparable interface to our class and override the CompareTo() method to sort a LinkedList with respect to specific items. After that, we need to use Collection.sort() method to sort a LinkedList.

Syntax: Collections.sort(Listobject)

Example:

Java




// Java Program to Sort LinkedList using Comparable
// in ascending order
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

Time Complexity: O(n log n)

Type 2: Descending Order

To sort a LinkedList in Descending order we have two Approaches.

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:

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

Time Complexity: O(n log n)

B. Override the CompareTo() method

In this approach, we have to override the compareTo() method in such a way that it will return a LinkedList in descending order.

Example:

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

Time Complexity: O(n log n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads