• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

compareTo()

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you right...

:-)
Andrej
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic