Collection.sort/reverse/reverOrder

22,085

Collections.reverse doesn't sort. It just reverse the order of the elements of the list. So if the list contains T, F, Z, it will contain Z, F, T.

Your code snipper initialize a comparator, but doesn't do anything with it. And this comparator only works, as the javadoc says, if the elements of the collection implement the Comparable interface.

You should either make the Song class implement the Comparable interface (look at its javadoc to know what this interface must do), or you must use a specific Comparator implementation. Whatever the chosen solution is, you'll have to implement some code to tell Collections.sort how the songs compare with each other. Is a song before another one when its title comes before the other song's title? Or is it when its duration is shorter than the other one?

See Sorting an ArrayList of Contacts based on name?

Share:
22,085
Pachun
Author by

Pachun

Updated on January 07, 2020

Comments

  • Pachun
    Pachun over 4 years

    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; 
        }
    
  • Pachun
    Pachun over 12 years
    Party Rock Anthem&Lmfao&327747 Last Friday Night&Katy Perry&370907 Tonight, Tonight&Hot Chelle Rae&355536 How To Love&Lil Wayne&279222 Pumped Up Kicks&Foster The People&355968 Super Bass&Nicki Minaj&223744 I Wanna Go&Britney Spears&267875 Give Me Everything&Pitbull&304190 Rolling In The Deep&Adele&315421 Good Life&OneRepublic&237007 Our job is to extract the title and artist and the time from this. I am using a separator to get the title, artist and time and I am storing this in an String array which has Title[] Artist[] and Time[], the time is converted to int after
  • Pachun
    Pachun over 12 years
    the Song class does implement Comparable<Song>
  • JB Nizet
    JB Nizet over 12 years
    If Song is comparable, use Collections.sort(list) to sort the list. Use Collection.sort(Collections.reverseOrder()) to sort the list in descending order. This is obvious if you read the javadoc.