aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compareTo( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compareTo( )" Watch "compareTo( )" New topic
Author

compareTo( )

K Raj Kumar
Greenhorn

Joined: Sep 16, 2008
Posts: 17
Hello Ranchers!
I m unable to understand/analyse a particular section of this code below:

class Song implements Comparable<Song>{
String title;
String artist;
String rating;

//1// public int compareTo(Song s){
//2// return title.compareTo(s.getTitle());
}
Song(String t, String a,String r){
title=t;
artist=a;
rating=r;
}

public String getTitle(){
return title;
}
public String getArtist(){
return artist;
}
public String getRating(){
return rating;
}
public String toString(){
return title;
}
}
The explanation given,(which I could not get accurately) beside the tags 1 and 2 in the code is as follows;

1- The sort() method sends a song to compareTo() to see how that Song compares to the Song on which the method was invoked.
2-We,just pass the work on to the title String objects , since we know Strings have a compareTo() method.

So,please Friends, help me out in understanding the concepts clearer.

Source: Head first java, Chapter 16 and Page 550.

I Thank in advance and will be much obliged
[ December 07, 2008: Message edited by: kasi.rajkumar ]

With Regards,<br />K.Raj Kumar.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Here the concept is:
Some how we have to compare a song with other song.
and we can compare songs based on differenc characteristics.
such as:
based on title
String title;
based on artist:
String artist;
based on rating: in window media player
String rating;

But we have to say how we want to compare a song.
And java needs object to implement comparable interface for this comparison.

//1// public int compareTo(Song s){

We have to compare a song with other so we are passing Song Object to
compareTo() method.

//2// return title.compareTo(s.getTitle());
We want to compare song by their title so we are using title for comparison.
If you want artist:
use: return artist.compareTo(s.getArtist());
for rating:
return rating.compareTo(s.getRating());

I think you are wondering on title.compareTo(s.getTitle());
title is a String Object and String class implements comparable interface.

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
public int compareTo(String anotherString);

String class compareTo() method takes another String for comparison.
Ans returns 0, negative or positive.
if both Strings are equal : 0 is returned.
if calling string is lexicographically less then argument string :
a value less than 0 negative is returned.
if calling string is lexicographically greater then argument string :
a value greater than 0 positive is returned.

Lexicographically order is may be natural order and depends on the ascii
value of the charaters in the string.
such as:
ascii for 'A' is 65
and for 'a' is 92
so "animal" > "Animal" by natural order.

"animal".compareTo("Animal") will return a value greater than 0 positive.
And Collections.sort() method uses this compareTo() to sort a list.

Hope this will help you.
[ December 07, 2008: Message edited by: Punit Singh ]

SCJP 6
K Raj Kumar
Greenhorn

Joined: Sep 16, 2008
Posts: 17
Hi Puneet,
Thanks for the response and a good explanation for my query.

I understood the comparing concept , the mechanism.
But, I m still confused with the "title"
You have said that String class has its own compareTo() method which takes another string for comparison.Now, s.getTitle() returns the title of the Song passed.
And den, this returned title is being compared to which Song title? That, I m not able to intrepret,Puneet.
[ December 07, 2008: Message edited by: kasi.rajkumar ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
And den, this returned title is being compared to which Song title? That, I m not able to intrepret,Puneet.


Hi RajKumar

If you make a list of songs:
List<Song> listOfSongs=new ArrayList<Song>();
listOfSongs.add(songObj1);
listOfSongs.add(songObj2);
listOfSongs.add(songObj3);

and use Collections.sort();
like:
Collections.sort(listOfSongs);

then sort() method in Collections class will call
songObj1.compareTo(songObj2);
songObj2.compareTo(songObj3);
etc......

means title of songObj2 will be compared to songObj1.
and songObj1's title will become the calling String Object and
songObj2's title will become Argument string passed to compareTo().

"songObj1.title".compareTo("songObj2.title");

hope this will help you.
K Raj Kumar
Greenhorn

Joined: Sep 16, 2008
Posts: 17
Yes,Puneet!
This explanation of yours has really helped me immensely.
Thanks for such a lucid and an illustrative explanation.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Welcome Raj
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Nice explanation Punit. Also, good command over api.

Thanks,

Naveen Katoch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compareTo( )
 
Similar Threads
Problems with comparable interface
Sorting on int field of object
Bad code - Help me fix it
Hashcode not removing duplicates from set
Head First Java HashSet