Java Comparable and Comparator Interface


            The Collections and Arrays can sorted using the method  in the API. Java.util.Colections,sort() and Arrays.sort() method is used to sort the Collections and Arrays. Remember that argument passed to Collections.sort() method must implement the Comparable interface otherwise we will get a compilation error.


The Comparable Interface:

J  This interface is used by Collections.sort() and Arrays.sort() method to sort the list and array of objects.

J  To implement this interface , a class must implement a single method compareTo().

J  Let us see  the  invocation of compareTo() method:

                        int  x= thisObject.compareTo(anotherObject);

            Here compareTo() to method returns an int     with the following meaning


1)    negative           ----- if thisObject  <   anotherObject

2)    zero                 ----- if thisObject == anotherObject

3)    positive            ----- if thisObject  >    anotherObject

J  Before generics the compareTo() method takes an Object rather than a specific type. Here we have to cast object to specific type. For example see  below code


        class DVDInfo implements Comparable {

                                    //some code

                        public int compareTo(Object o){


                                    DVDInfo d= (DVDInfo) o ;

                                    return title.compareTo(d.getTitle));




The Comparator Interface

J     The Comparator interface gives you  the capability to sort collection in different ways.

J     This interface is used sort instance of any class even if classes you can't modify.

J     To implement this interface , a class must implement a single method compare().

J method returns an int whose meaning is same as Comparable.compareTo() method's return value.


Comparing Comparable to Comparator :




Int objeone.compareTo(objTwo);

Int compare(objOne, objTwo)


            negative    if objOne< objTwo

            zero           if objOne == objTwo

            positive     if objOne  >  objTwo


Same as Comparable

We must modify the class whose instances we want to sort.

We build a class separate from the class whose instances we want to sort

Only one sort sequence can be created

Many sort sequence can be created

Implemented frequently in the API by:

String, Wrapper classes, Date, Calender....

Meant to be implemented to sort instances of third party classes.




Let us see the example for both  Comparator and Comparable.


import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;



public class Student implements Comparable<Student> {

            private String name;

            private int score;


            public Student(String name, int score) {

               = name;

                        this.score = score;



            public int compareTo(Student obj){

                        Integer i=new Integer(score);

                        return i.compareTo(obj.score);




            public static void main(String args[]) {

                 ArrayList<Student> list = new ArrayList<Student>();


                 list.add(new Student("Ann", 87));

                 list.add(new Student("Bob", 83));

                 list.add(new Student("Cat", 99));

                 list.add(new Student("Dan", 25));

                 list.add(new Student("Eve", 76));


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

                 while (iter.hasNext()) {

                    Student s =;

                    System.out.println( + "  " + s.score);


                 System.out.println("Sorted List");


                 Iterator<Student> iter1 = list.iterator();

                 while (iter1.hasNext()) {

                    Student s =;

                    System.out.println( + "  " + s.score);



                 StudentName st1= new StudentName();


                 Iterator<Student> iter2 = list.iterator();

                 while (iter2.hasNext()) {

                    Student s =;

                    System.out.println( + "  " + s.score);





            static class StudentName implements Comparator<Student>{

                        public int compare(Student o1, Student o2) {






Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on ; I would like to hear from you :)