• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with comparable interface  RSS feed

 
Ayan Biswas
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this code...

Here we are adding the song objects in the arraylist.Now ,while sorting we are calling sort() method where as a parameter we are passing the entire arraylist of song objects.But how does sort() call compareTo() on a particular song object?And how does work? which song objects title are we comparing with s.getTtitle();?? Please explain in details
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you find this code? Please QuoteYourSources.

This code won't compile because there are several things wrong with it.

To answer your first question, the sort method expects a list of Comparable objects, so it knows it can call the compareTo() method on them.
 
Ayan Biswas
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this from "Head First Java " book.But I gave only a part of the code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!