Comparable Interface in Java

Introduction

 
In this particular article, we will discuss the Comparable interface in Java, with some simple examples, so that it is easier to understand the concepts.
  

Comparable interface in Java

 
Basically, the comparable interface is used for ordering the user-defined classes. This interface is found in java.lang package.  It contains only one method named CompareTo(object) and it is the property, by which comparisons and sorting can be done. If a class implements a comparable interface, then a collection of its objects can be sorted using the Collection.sort() method and an array of objects, containing the class's objects as elements, can be sorted using the Array.sort() method.
 
We can sort the following elements:
  • User-defined class object
  • String object
  • Wrapper class object
String and wrapper classes implement the comparable interface. So, if you store the objects of the wrapper or string classes, then it will be comparable.
 
Some syntax that is used in the Comparable interface:
  • Collection.sort(): It is used to sort the collection of a class's objects.
  • Array.sort(): It is used to sort the elements of a class's objects.
  • Compare.To(): It is used to compare the current object with a specified object.
  • Void.sort(): It is used to sort the elements of a list.  The list should be of comparable type.

 
The following are examples of sorting a collection of Java objects, using the comparable interface.

 
Here we are creating two Java files.  One is Employee.java and the other is Main.java.
 

Employee.java 

  1. class Employee implements Comparable {  
  2.  int id;  
  3.  String name;  
  4.  int age;  
  5.  String address;  
  6.  int salary;  
  7.  Employee(int id, String name, int age, String address, int salary) {  
  8.   this.id = id;  
  9.   this.name = name;  
  10.   this.age = age;  
  11.   this.address = address;  
  12.   this.salary = salary;  
  13.  }  
  14.  public int compareTo(Object obj) {  
  15.   Employee E = (Employee) obj;  
  16.   if (age == E.age)  
  17.    return 0;  
  18.   else if (age > E.age)  
  19.    return 1;  
  20.   else  
  21.    return -1;  
  22.  }  
  23. }  
  24. Main.java  
  25. import java.util.*;  
  26. class Main {  
  27.  public static void main(String args[]) {  
  28.   ArrayList EL = new ArrayList();  
  29.   EL.add(new Employee(1001"Shubham"19"Jhansi"5000));  
  30.   EL.add(new Employee(1002"Akash"23"Lucknow"10000));  
  31.   EL.add(new Employee(1003"Ravi"34"Kanpur"12000));  
  32.   EL.add(new Employee(1004"Shashikant"21"Delhi"15000));  
  33.   Collections.sort(EL);  
  34.   Iterator itr = EL.iterator();  
  35.   while (itr.hasNext()) {  
  36.    Employee s = (Employee) itr.next();  
  37.    System.out.println(s.id + " " + s.name + " " + s.age + " " + s.address + " " + s.salary);  
  38.   }  
  39.  }   
In the preceding example, we sorted the employee list based on their age, the output is as follows:
 
Output
 
Output
 
If we sort the employee list based on their salary, the output will be as follows:
 
Output
 
sort the employee
 
And, if we sort the employee list based on their id, then the output will be as follows:
 
Output
 
ort the list
 
You can also sort the list depending on their name or address.