wood burning stoves 2.0
The moose likes Beginning Java and the fly likes compareTo() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compareTo()" Watch "compareTo()" New topic


vibhas karn

Joined: Aug 11, 2008
Posts: 26
Hello friends its Vibhas can anybody explain me my doubts in this following code and also please cheak my comments wheather my understanding is correct or not...
My doubt part is marked as //??? part which is the last line please clarify it here is the code

import java.util.*;

public class Dvdinfo implements Comparable<Dvdinfo>
String title;
String genre;
String leadActor;

Dvdinfo(String t,String g,String k)
this.title = t;
this.genre = g;
this.leadActor = k;

public static void main(String[] args)
Dvdinfo dvi1= new Dvdinfo("sholay","male","Amitabh");
Dvdinfo dvi2= new Dvdinfo("Charas","male","jimmy");
Dvdinfo dvi3= new Dvdinfo("Lagaan","male","Aamir");


ArrayList<Dvdinfo> ard = new ArrayList<Dvdinfo>();

Collections.sort(ard);// here the collection of list( i.e what "ard" contains) is not
//of type String but of type Dvdinfo so sorting will not be performed
// automatically we have to override the compareTo() method of
//Comparable interface i.e by implementing Compareable interface
//So it will search for the type " public int compareTo(Dvdinfo d)"
// in the following code so it can perform sorting hence compareTo() is
// called.....

System.out.println("Collections :"+ard);


public String toString()
return title + " " + genre + " " + leadActor +"\n";

public int compareTo(Dvdinfo d)
System.out.println(title +" --- "+d.getTitle());

return title.compareTo(d.getTitle());
// title takes the 1st title value "sholay" and d.getTitle() returns 2nd
// title i.e "Charas" similarly comparision of "sholay" and "Laagan"
// and comparision of "Charas" and "Lagaan" and after that compareTo()
// method sort this titles


public String getTitle()
return title;


// ??? Can anybody explain me how compareTo method is basically working i.e
// how java API is implementing this compareTo() method
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
The programmer has to implement the compareTo method of the Comparable interface and return a negative integer, 0 or a positive integer based on whether the receiving (this) object is less, equal or greater than the spcecified object for a class that has no natural order (i.e. Comparable not implemented).

In your particular implementation of the compareTo method you are taking advantage of the natural ordering of the String class to return these integer values.

Take a look here for a extensive explanation. One of the many links I've picked up from Campbell..
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

You should read the API for Coparable.

Basically, compareTo returns an integer value. The value should represent the order of the objects.

If the implementing (this) object should come first in the series then the return value should be < 0.

If the parameter to the compareTo method should come first in the series then the return should be > 0.

If the parameter and the implementing (this) object are equal in case of the comparison then the returned value should be zero.

What the Collections.sort method does is create a new list of objects. It then iterates over the original list and calls the compareTo on it and the value already present in the new list. If the value of compareTo is <= 0, it puts the value of the original list before the value in the new list, otherwise it puts the original value in the new list at some point after the sorted value. It then copies the objects from the sorted list into the original list in the now sorted order.

The actual method in Collections.sort uses an array with a Merge Sort algorithm which will be more efficient, but the above code gives you an idea of how the compareTo method would be called.

vibhas karn

Joined: Aug 11, 2008
Posts: 26
Thanks Steve for your pseudocode its really helpful. Can you give me the example that how should i implement this same program using compare method of Comparator interface or add few lines in it so that i could understand how the compareTo() differs from compare().
Basically I am not able to make it out.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46393
Yes, it is a useful explanation. But when you go to the API for Comparable<T> it says something implementing Comparable implies a "natural order." Does a Dvdinfo class really have a "natural order?" It might be better to create Comparators, so you can sort DVDs by their title, artist, etc.
vikram veera
Ranch Hand

Joined: Dec 26, 2000
Posts: 43
You can find the code with example in the below URL.It will be more useful.
Using the Comparable and Comparator interfaces to sort data in Java

Andreas Hollmann

Joined: Jan 06, 2010
Posts: 27

Collections.sort converts the collections to an array, then sorts the array and then overrides the list elements with the elements from the sorted array. The standard Arrays.sort(...) method however is implemented as single-thread sorting algorithm and doesn't use many cores of your system. I advise to use happy-Colelctions (Apache License 2.0) to sort your list.

Or by using Comparator

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46393
Welcome to JavaRanch

Your habit of opening old threads and recommending your own product could very easily be mistaken for spamming. Please be careful what you are recommending.
Andreas Hollmann

Joined: Jan 06, 2010
Posts: 27
yes you right...

Consider Paul's rocket mass heater.
subject: compareTo()
It's not a secret anymore!