Site icon JavaGoal

How to perform linked list sorting

In java, we have how to perform some basic operations on Java LinkedList. But there may be a situation when we want to perform some complex on LinkedList like how to sort a linked list. In this post, we will see the sorting of LinkedList objects that totally depends on Comparable or Comparator.

Here is the table content of the article will we will cover this topic.
1. LinkedList.sort(Comparator c)
2. Collections.sort(Comparator c)

LinkedList.sort(Comparator c)

The sort() method is defined in the List interface and it is a default method. Let’s take an example of LinkedList and sort the objects of LinkedList. If you are not familiar with the Comparator interface then read the Comparator interface first. Suppose we have a LinkedList with some string objects and objects are not in sorted order.
We can sort the LinkedList by sort(Comparator c) method that will sort the Strings alphabetically in ascending order. Let’s see the linked list sorting

public void sort(Comparator<E> c) 

void, This method doesn’t return anything because it’s return type is void.
c, Object of comparator

import java.util.Comparator;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
	public static void main(String[] args) 
	{
		LinkedList<String> listOfNames = new LinkedList<String>();
		
		listOfNames.add("D");
		listOfNames.add("A");
		listOfNames.add("Z");
		listOfNames.add("Y");
		listOfNames.add("X");
		
		System.out.println("Before sorting: "+ listOfNames);
		listOfNames.sort(new MyComparator());
		System.out.println("After sorting: "+ listOfNames);
	}
}

class MyComparator implements Comparator<String>
{
	public int compare(String string1, String string2) 
    { 
        return string1.compareTo(string2); 
    } 
}

Output: Before sorting: [D, A, Z, Y, X]
After sorting: [A, D, X, Y, Z]

Sorting of LinkedList contains User-defined class objects

Suppose we have a LinkedList that containing the object of User-defined class that is Student. and we want to sort the LinkedListon based on different criteria. Let’s see linked list sorting by linked list program in java.

import java.util.Comparator;
import java.util.LinkedList;
class Student
{  
    int rollNo;  
    String name;  
    int age;  
    Student(int rollno,String name,int age)
    {  
        this.rollNo=rollno;  
        this.name=name;  
        this.age=age;  
    }  
}
class AgeComparator implements Comparator<Student>
{  
    @Override
    public int compare(Student student1, Student student2) 
    {
        if(student1.age==student2.age)  
        return 0;  
        else if(student1.age>student2.age)  
        return 1;  
        else  
        return -1;  
    }  
}  
class RollNoComparator implements Comparator<Student>
{  
    @Override
    public int compare(Student student1, Student student2) 
    {
        if(student1.rollNo==student2.rollNo)  
        return 0;  
        else if(student1.rollNo>student2.rollNo)  
        return 1;  
        else  
        return -1;  
    }  
}  
public class ExampleOfLinkedList
{  
    public static void main(String args[])
    {  
        LinkedList<Student> listOfStudent = new LinkedList<Student>();  
        listOfStudent.add(new Student(1,"Ravi",26));  
        listOfStudent.add(new Student(2,"kant",27));  
        listOfStudent.add(new Student(3,"kamboj",20));  
        
        // It Sorts all the objects based on Age 
        listOfStudent.sort(new AgeComparator()); 
        System.out.println("Sorting based on Age:");
        for(Student student:listOfStudent)
        {  
            System.out.println("RollNo of Student = "+student.rollNo);
            System.out.println("Age of Student = "+student.age);
            System.out.println("Name of Student = "+student.name);
        }
        
        // It Sorts all the objects based on RollNo 
        listOfStudent.sort(new RollNoComparator());
        System.out.println("Sorting based on RollNo:");
        for(Student student:listOfStudent)
        {  
             System.out.println("RollNo of Student = "+student.rollNo);
             System.out.println("Age of Student = "+student.age);
             System.out.println("Name of Student = "+student.name);
        }
    }  
}  

Output: Sorting based on Age:
RollNo of Student = 3
Age of Student = 20
Name of Student = kamboj
RollNo of Student = 1
Age of Student = 26
Name of Student = Ravi
RollNo of Student = 2
Age of Student = 27
Name of Student = kant
Sorting based on RollNo:
RollNo of Student = 1
Age of Student = 26
Name of Student = Ravi
RollNo of Student = 2
Age of Student = 27
Name of Student = kant
RollNo of Student = 3
Age of Student = 20
Name of Student = kamboj

Collections.sort(Comparator c)

There is another method provided by the Collections class. We can sort the LinkedList by use of the Collections.sort(Comparator c) method.

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
	public static void main(String[] args) 
	{
		LinkedList<String> listOfNames = new LinkedList<String>();
		
		listOfNames.add("D");
		listOfNames.add("A");
		listOfNames.add("Z");
		listOfNames.add("Y");
		listOfNames.add("X");
		
		System.out.println("Before sorting: "+ listOfNames);
		Collections.sort(listOfNames);
		System.out.println("After sorting: "+ listOfNames);
	}
}

class MyComparator implements Comparator<String>
{
	public int compare(String string1, String string2) 
    { 
        return string1.compareTo(string2); 
    } 
}

Output: Before sorting: [D, A, Z, Y, X]
After sorting: [A, D, X, Y, Z]

Sorting of LinkedList contains User-defined class objects

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
class Student
{  
    int rollNo;  
    String name;  
    int age;  
    Student(int rollno,String name,int age)
    {  
        this.rollNo=rollno;  
        this.name=name;  
        this.age=age;  
    }  
}
class AgeComparator implements Comparator<Student>
{  
    @Override
    public int compare(Student student1, Student student2) 
    {
        if(student1.age==student2.age)  
        return 0;  
        else if(student1.age>student2.age)  
        return 1;  
        else  
        return -1;  
    }  
}  
class RollNoComparator implements Comparator<Student>
{  
    @Override
    public int compare(Student student1, Student student2) 
    {
        if(student1.rollNo==student2.rollNo)  
        return 0;  
        else if(student1.rollNo>student2.rollNo)  
        return 1;  
        else  
        return -1;  
    }  
}  
public class ExampleOfLinkedList
{  
    public static void main(String args[])
    {  
        LinkedList<Student> listOfStudent = new LinkedList<Student>();  
        listOfStudent.add(new Student(1,"Ravi",26));  
        listOfStudent.add(new Student(2,"kant",27));  
        listOfStudent.add(new Student(3,"kamboj",20));  
        
        // It Sorts all the objects based on Age 
        Collections.sort(listOfStudent, new AgeComparator());  
        for(Student student:listOfStudent)
        {  
            System.out.println("RollNo of Student = "+student.rollNo);
            System.out.println("Age of Student = "+student.age);
            System.out.println("Name of Student = "+student.name);
        }
        
        // It Sorts all the objects based on RollNo 
                Collections.sort(listOfStudent, new RollNoComparator());  
                for(Student student:listOfStudent)
                {  
                    System.out.println("RollNo of Student = "+student.rollNo);
                    System.out.println("Age of Student = "+student.age);
                    System.out.println("Name of Student = "+student.name);
                }
    }  
}  

Output: RollNo of Student = 3
Age of Student = 20
Name of Student = kamboj
RollNo of Student = 1
Age of Student = 26
Name of Student = Ravi
RollNo of Student = 2
Age of Student = 27
Name of Student = kant
RollNo of Student = 1
Age of Student = 26
Name of Student = Ravi
RollNo of Student = 2
Age of Student = 27
Name of Student = kant
RollNo of Student = 3
Age of Student = 20
Name of Student = kamboj

Exit mobile version