I got this code form "Head First Java".
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
return title.compareTo (s.getTitle());
work? which song objects title are we comparing with s.getTtitle();? ?
Code:
class Song implements Comparable<Song>{
String art;
String tit;
Song(String a,String t)
{
tit=t;
art=a;
}
public int compareTo(Song s)
{
return title.compareTo(s.getTitle());
}
String getTitile()
{}
String getAuthor()
{}
}
class Songsort{
Arraylist <Song> sl=new Arraylist<Song>();
public static void main(String arg[])
{new Songsort.go();
}
void go(){
getSong();
Collections.sort(sl);}
}
void getSong(){
//..i/o code
}
}
return title.compareTo (s.getTitle());
work? which song objects title are we comparing with s.getTtitle();? ?
Comment