Java Program to Sort LinkedList using Comparable
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:
- Ascending Order
- 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
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
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 public int compareTo(Student s)
{
if (Rank > s.Rank) {
return 1 ;
}
else if (Rank == s.Rank) {
return 0 ;
}
else {
return - 1 ;
}
}
}
public class Main {
public static void main(String[] args)
{
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 ));
System.out.println( "UnSorted List" );
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
Collections.sort(List);
System.out.println( "Sorted List" );
for (Student s : List) {
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
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
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 public int compareTo(Student s)
{
if (Rank > s.Rank) {
return 1 ;
}
else if (Rank == s.Rank) {
return 0 ;
}
else {
return - 1 ;
}
}
}
public class Main {
public static void main(String[] args)
{
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 ));
System.out.println( "UnSorted List" );
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
Collections.sort(List, Collections.reverseOrder());
System.out.println( "Sorted List" );
for (Student s : List) {
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
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
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 public int compareTo(Student s)
{
if (Rank < s.Rank) {
return 1 ;
}
else if (Rank == s.Rank) {
return 0 ;
}
else {
return - 1 ;
}
}
}
public class Main {
public static void main(String[] args)
{
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 ));
System.out.println( "UnSorted List" );
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
Collections.sort(List);
System.out.println( "Sorted List" );
for (Student s : List) {
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)
Last Updated :
26 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...