aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compareTo()" Watch "compareTo()" New topic
Author

compareTo()

vibhas karn
Greenhorn

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");

System.out.println(dvi1);
System.out.println(dvi2);
System.out.println(dvi3);


ArrayList<Dvdinfo> ard = new ArrayList<Dvdinfo>();
ard.add(dvi1);
ard.add(dvi2);
ard.add(dvi3);


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: 350
Actually,
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
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
vibhas karn
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Thanks
Vikram
www.developerparadise.com
Andreas Hollmann
Greenhorn

Joined: Jan 06, 2010
Posts: 27
Collections.sort(ard);

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
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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
Greenhorn

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

:-)
Andrej
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compareTo()