Java collection Sorting Interfaces

These sorting interfaces are found in java.lang package

Sorting of Collections: using

  1. Comparable Interface : used for sorting
    1. String objects
    2. Wrapper class objects
    3. User-defined class objects
  2. Comparator Interface : used for sorting
    1. User-defined class objects

Comparable Interface


Java Comparable interface is used to sort or order the objects of the user-defined class. This interface contains only one method named compareTo(Object). It provides a single sorting sequence only i.e. you can sort the elements on the basis of single data member only. For example, it may be rollno, name, age or anything else.

public int compareTo(Object obj): It is used to compare the current object with the specified object. It returns

  1. positive integer, if the current object is greater than the specified object.
  2. negative integer, if the current object is less than the specified object.
  3. zero, if the current object is equal to the specified object.

Note : String class and Wrapper classes implement the Comparable interface by default. So if you store the objects of string or wrapper classes in a list, set or map, it will be Comparable by default.Example :

Of the Comparable interface that sorts the list elements on the basis of age.

                  
class Student implements Comparable<Student>{  
  int rollno;  
  String name;  
  int age;  
  Student(int rollno,String name,int age){  
  this.rollno=rollno;  
  this.name=name;  
  this.age=age;  
}  
  
public int compareTo(Student st){  
  if(age==st.age)  
    return 0;  
    else if(age>st.age)  
    return 1;  
    else  
    return -1;  
  }  
}  

import java.util.*;  
public class TestSort1{  
public static void main(String args[]){  
  ArrayList<Student> al=new ArrayList<Student>();  
  al.add(new Student(101,"Vijay",23));  
  al.add(new Student(106,"Ajay",27));  
  al.add(new Student(105,"Jai",21));  
  
Collections.sort(al);  
for(Student st:al){  
    System.out.println(st.rollno+" "+st.name+" "+st.age);  
    }  
  }  
} 

Comparator Interface


This interface contains 2 methods compare(Object obj1,Object obj2) and equals(Object element). It provides multiple sorting sequences, i.e., you can sort the elements on the basis of any data member, for example, rollno, name, age or anything else.

public void sort(List list, Comparator c): is used to sort the elements of List by the given Comparator.

  1. positive integer, if the current object is greater than the specified object.
  2. negative integer, if the current object is less than the specified object.
  3. zero, if the current object is equal to the specified object.

Note : String class and Wrapper classes implement the Comparable interface by default. So if you store the objects of string or wrapper classes in a list, set or map, it will be Comparable by default.Example :

Of the Comparable interface that sorts the list elements on the basis of age.

                  
class Student{  
  int rollno;  
  String name;  
  int age;  
  Student(int rollno,String name,int age){  
  this.rollno=rollno;  
  this.name=name;  
  this.age=age;  
  }  
}  

import java.util.*;  
class AgeComparator implements Comparator<Student>{  
  public int compare(Student s1,Student s2){  
    if(s1.age==s2.age)  
    return 0;  
    else if(s1.age>s2.age)  
    return 1;  
    else  
    return -1;  
  }  
}  

import java.util.*;  
import java.io.*;  
class Simple{  
public static void main(String args[]){  
  
ArrayList<Student> al=new ArrayList<Student>();  
al.add(new Student(101,"Vijay",23));  
al.add(new Student(106,"Ajay",27));  
al.add(new Student(105,"Jai",21));  
  
System.out.println("Sorting by Name");  
  
Collections.sort(al,new NameComparator());  
for(Student st: al){  
System.out.println(st.rollno+" "+st.name+" "+st.age);  
}  
  
System.out.println("Sorting by age");  
  
Collections.sort(al,new AgeComparator());  
for(Student st: al){  
System.out.println(st.rollno+" "+st.name+" "+st.age);  
}  
}  

}  

Summary

In this article we learnt about Java collections sorting and its various types. We learnt about Comparable and Comparator interface.
Hope you liked the article !


Leave a Comment