How to Override compareTo Method in Java? – GeeksforGeeks

As we know, there are basically two types of sorting technique in Java:

  • First is internal sorting i.e that uses predefined sorting method ascending order Arrays.sort() for Primitive class arrays and wrapper class arrays and  Collections.sort() for collections both methods sort the elements in ascending order.
  • The second technique is for sorting the elements is using the comparator or comparable interface in a class.
    • Comparator Interface: Implement the comparator interface in the class and override compare() method or pass the new comparator as the second argument in the sorting methods and change the sorting order according to the requirements. Comparator only works for wrapper type arrays and for collections like vector, ArrayList, etc.
       
    • Comparable Interface: This interface implements a single sorting technique, and it affects the whole class. The comparable interface provides a compareTo() method to sort the elements.

To summarize, in Java, if sorting of objects needs to be based on natural order then use the compareTo() method of Comparable Interface. For Integers default natural sorting order is ascending and for Strings it is alphabetical. Whereas, if you’re sorting needs to be done on attributes of different objects, or customized sorting then use compare() of Comparator Interface.

Overriding of the compareTo() Method

In order to change the sorting of the objects according to the need of operation first, we have to implement a Comparable interface in the class and override the compareTo() method. Since we have to sort the array of objects, traditional array.sort() method will not work, as it used to work on primitive types, so when we call the Arrays.sort() method and pass the object array, it will search, whether we have overridden the compareTo() method or not. Since we have overridden the compareTo() method, so objects will be compared by using this compareTo() methods, based on the age.

Java




import java.util.*;

 

public class GFG implements Comparable<GFG> {

 

    String name;

    int age;

 

    

    GFG(String name, int age)

    {

        this.name = name;

        this.age = age;

    }

    public int getage() { return age; }

    public String getname() { return name; }

    public static void main(String[] args)

    {

        

        GFG ob[] = new GFG[4];

 

        

        ob[0] = new GFG("Aayush", 14);

        ob[1] = new GFG("Ravi", 12);

        ob[2] = new GFG("Sachin", 19);

        ob[3] = new GFG("Mohit", 20);

 

        

        Arrays.sort(ob);

 

        for (GFG o : ob) {

 

            

            

            System.out.println(o.name + " " + o.age);

        }

 

        

        

        ArrayList<GFG> objects = new ArrayList<>();

 

        

        GFG newObject1 = new GFG("Rohan Devaki", 20);

 

        

        objects.add(newObject1);

 

        

        GFG newObject2 = new GFG("Algorithammer", 22);

 

        

        objects.add(newObject2);

 

        

        Collections.sort(objects);

 

        for (GFG o : objects) {

            

            

            System.out.format("%s  %d\n", o.name, o.age);

        }

    }

    

    @Override public int compareTo(GFG o)

    {

        if (this.age > o.age) {

 

            

            return 1;

        }

        else if (this.age < o.age) {

 

            

            return -1;

        }

        else {

 

            

            return 0;

        }

    }

}



Output

Ravi 12
Aayush 14
Sachin 19
Mohit 20
Rohan Devaki  20
Algorithammer  22

My Personal Notes

arrow_drop_up