I have an Arraylist of songs which store title, artist and time I have all my code written up and everything. I just wanted to know more about Collection.sort and Collection.reverse and Collection.reverseOrder. I have a file that has all the songs and everything. I want to sort the songs according to descending order according to time.
When I try this I either get an error or it doesnt sort properly. can anyone suggest how I cant use Collection.sort and use the comparator
Comparator<Song> comparator = Collections.reverseOrder();
Collections.reverse(listOfSongs);
My compare method is as follows:
public int compare(Song mySong1,Song mySong2 ){
if (mySong1.getLength() > mySong2.getLength()){
return -1;
}
if(mySong1.getLength() < mySong2.getLength()){
return 1;
}
if(mySong1.getLength() == mySong2.getLength())
{
if(mySong1.getTitle().compareTo(mySong2.getTitle()) > 0){
return -1;
}
if(mySong1.getTitle().compareTo(mySong2.getTitle()) < 0 ){
return 1;
}
else {
if(mySong1.getComposer().compareTo(mySong2.getComposer()) >0){
return -1;
}
if(mySong1.getComposer().compareTo(mySong2.getComposer()) <0) {
return 1;
}
}
}
return 0;
}