Take the 2-minute tour ×

Here is my Code

class ComparableTest
    public static void main(String[] args)
        BOX[] box = new BOX[5];
        box[0] = new BOX(10,8,6); 
        box[1] = new BOX(5,10,5);
        box[2] = new BOX(8,8,8);
        box[3] = new BOX(10,20,30);
        box[4] = new BOX(1,2,3);
        for(int i=0;i<box.length;i++)

Also i have a class BOX that implements Comparable. Now i have a few question that i would like you all to help me out with.

1.Are the methods declared in comparable interface,system defined, as in can i have any method in the comparable, or it has to be compareTo only?

2.I did not provide the implementation of Arrays.sort method, how does it sort my elements then?

3.When i use Comparator instead of comparable, then I use:

class comparatorTest
    public static void main(String args[])
        Student[] students = new Student[5];
        Student[0] = new Student(“John”,”2000A1Ps234”,23,”Pilani”);
        Student[1] = new Student(“Meera”,”2001A1Ps234”,23,”Pilani”);
        Student[2] = new Student(“Kamal”,”2001A1Ps344”,23,”Pilani”);
        Student[3] = new Student(“Ram”,”2000A2Ps644”,23,”Pilani”);
        Student[4] = new Student(“Sham”,”2000A7Ps543”,23,”Pilani”);    
        // Sort By Name
        Comparator c1 = new studentbyname();
        for(int i=0;i<students.length;i++)

//In the above code, studentbyname implements comparator, but box stil implements comparable .i.e

class studentbyname implements comparator
    public int compare(Object o1,Object o2)
        Student s1 = (Student) o1;
        Student s2 = (Student) o2;
        return s1.getName().compareTo(s2.getName());

Now i am doing Arrays.sort(students,c1), why am i passing c1 now?

share|improve this question
You don't have basic knowledge on how polymorphism ,inheritance and method overloading work in java. Look those up first, and you'll understand why: u need to implement compareTo, you can pass a different number of arguments to what seems to be the same method. –  Shivan Dragon Sep 21 '11 at 14:58
add comment

3 Answers

up vote 2 down vote accepted
  1. In order to meet the Comparable contract, you must have at least the compareTo method. You may have as many addition methods in your class as you would like.

  2. It sorts the elements in the natural order based on the Comparable interface. So sort is calling compareTo between the elements to see in which order to place them.

  3. Providing a Comparator to the sort method allows sort to order elements that either (a) don't implement Comparable or (b) where you want to order them in some other order than the "natural order" as defined by the class's Comparable implementation.

When you pass a Comparator to sort, it calls the Comparator's compare method rather than the elements' compareTo method (if implemented).

see What is an interface

see Comparator

see Comparable

share|improve this answer
so Comparable is like, i dont know what the right term would be, but 'system defined' that is, it has to contain compareTo right? –  Kraken Sep 21 '11 at 15:00
Comparable is an interface, which means that it defines a set of methods that must be implemented by any class that claims to implement the interface. –  John B Sep 21 '11 at 15:01
and what happens when i pass 'c' along with the array to be sorted? –  Kraken Sep 21 '11 at 15:02
Yes you may define any interface you like with any set of methods. And yes, Comparable is an already defined system interface. See my links. –  John B Sep 21 '11 at 15:07
Generally, Comparator implementation would not use a classes Comparable implementation. What would be the point since they would result in the same order and passing a Comparator to sort is optional. Most implementation of Comparator use some different comparison mechanism to order the elements of a class. It usually uses getters to retrieve fields from the class upon which to base the comparison. –  John B Sep 21 '11 at 15:17
show 3 more comments
  1. You can define as many methods as you want in a Comparable, as long as you implement compareTo. This method can be used in many situations where the class is checked for comparation. For example, when inserting instances into an ordered TreeSet. compareTo provides a general ordering rule for all instances of the class.
  2. Arrays.sort orders the array in the natural order of its elements. That is, using compareTo.
  3. You can use Comparator to define a custom ordering rule. Like when you have a table you can sort by any of its columns. You could define a Comparator for each of its columns, and you could still have a class-inherent ordering rule (as in related to the reality the Class represents) defined in the class' compareTo.
share|improve this answer
add comment

Implementing Comparable obligates you to provide an implementation for compareTo().

All elements in the Object array passed to the Arrays.sort(Object[]) method must implement Comparable. If you want to use a Comparator instead, you have to use an Arrays.sort() method that takes a Comparator as a parameter. The method you use in your example above takes a Comparator as the second parameter - hence the need to provide c1 in the method call.

share|improve this answer
add comment

Your Answer


