Home > DeveloperSection > Articles > Comparable example in Java

Comparable example in Java

Java Java 
1 Comment(s)
 20043  View(s)
Rate this:

Comparable example in Java

Java provides the Comparable interface, which contains only one method, called compareTo. This method compares two objects, in order to impose an order between them. Specifically, it returns a negative integer, zero, or a positive integer to indicate that the input object is less than, equal or greater than the existing object.

Comparable interface is used to order the objects of user-defined class.This interface is found in java.lang package and contains only one method named compareTo(Object).It provide only single sorting sequence i.e. you can sort the elements on based on single data member only.For instance it may be either rollno,name,age or anything else.

Here I am creating a sample of sorting using Comparable interface in java.



import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;


class Sample {



     public static void main(String args[]) {

          // create object of Mathematical class

          ArrayList myList = new ArrayList();


          myList.add(new Student(101, "Zack", 22));

          myList.add(new Student(104, "Anderson", 26));

          myList.add(new Student(103, "Joolie", 24));

          myList.add(new Student(106, "Raakel", 28));

          myList.add(new Student(105, "Abbas", 23));

          myList.add(new Student(102, "Ukrit", 21));

          myList.add(new Student(108, "Abbe", 29));

          myList.add(new Student(107, "1234", 29));



          Iterator<Student> iter = myList.iterator();

          System.out.println("Sorting by UserId\n");

          while (iter.hasNext()) {

              Student student = (Student) iter.next();

            System.out.println(student.sId + "     " + student.Sname);







class Student implements Comparable {

     int sId;

     String Sname;

     int age;


     public Student(int id, String name, int age) {

          this.sId = id;

          this.Sname = name;

          this.age = age;




     public int compareTo(Object obj) {

          Student st = (Student) obj;

          if (sId == st.sId)

              return 0;

          else if (sId > st.sId)

              return 1;


              return -1;





Sorting by UserId


101     Zack

102     Ukrit

103     Joolie

104     Anderson

105     Abbas

106     Raakel

107     1234

108     Abbe


Difference between Comparable and comparator


1.      Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc.

2.      Comparable affects the original class i.e. actual class is modified.

3.      Comparable provides compareTo() method to sort elements.

4.      Comparable is found in java.lang package.

5.      We can sort the list elements of Comparable type by Collections.sort(List) method.


1.       Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc.

2.       Comparator doesn't affect the original class i.e. actual class is not modified.

3.       Comparator provides compare() method to sort elements.

4.       Comparator is found in java.util package.

5.       We can sort the list elements of Comparator type by Collections.sort(List,Comparator) method.

Comparable VS comparator

By Samuel Fernandes on   one year ago
Good Comparison!!

Don't want to miss updates? Please click the below button!

Follow MindStick