Java Java Data Structures Efficiency! Custom Serialization

Mojo Waters
Mojo Waters
5,748 Points

Hi I get this message. The method sort(new Comparator<Song>(){}) is undefined for the type List<Song>

The method sort(new Comparator<Song>(){}) is undefined for the type List<Song>

5 Answers

Walter Allen
Walter Allen
iOS Development with Swift Techdegree Student 16,021 Points

Hey, Mojo! Thanks for reaching out for help. In order to provide the best answer, we would need to see your actual code, not just the error, to help you sort out the meaning of this error message. Could you paste your code here in the Q so that we can look over it and see if we can offer assistance? :)

However, looking at this, it appears that you might have declared a List<Song> variable and tried to sort it. The List<E> interface does not have a sort method as part of its implementation. Therefore, you would either need to construct a sort method for it in your own code or change the declaration to a class that actually has a sort method. Does that help any?

Mojo Waters
Mojo Waters
5,748 Points

Hi Walter,

Thanks for your answer. I kind of get what you mean, but how should I handle it? Craig just imported import java.util.Comparator and it worked?

package musicbox.model;

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.Comparator; import java.util.Arrays;

import musicbox.model.Song; public class SongBook {

private List <Song> mSongs; 

public SongBook () {
    mSongs = new ArrayList<Song>(); 

} 

public void exportTo(String filename) {

    try (
        FileOutputStream fos = new FileOutputStream(filename);
        PrintWriter writer = new PrintWriter(fos);
            )
            {
                for(Song song: mSongs) {
                    writer.printf("%s|%s|%s%n",
                            song.getmArtists(),
                            song.getmTitle(),
                            song.getmVideoUrl());
                }
            }   
    catch(IOException ioe) {
        System.out.printf("Problem saving %s %n",filename);
    ioe.printStackTrace();
    }


}

public void ImportFrom(String filename) {

    try(FileInputStream fis = new FileInputStream(filename);
            BufferedReader reader = new BufferedReader (new InputStreamReader(fis));
)
        {
         String line; 
         while((line = reader.readLine()) != null) {
             String[] args = line.split("\\|");
             addSong(new Song(args[0],args[1],args[2]));
         }

        }

catch(IOException ioe) {
    System.out.printf("Problem loading  %s %n",filename);
    ioe.printStackTrace();
}
} 


public void addSong(Song song) {
    mSongs.add(song); 

} 

public int getSongCount() {
    return mSongs.size();

}

private Map <String,List<Song>> byArtist() {

    Map<String, List<Song>> byArtist = new TreeMap<>();
    for (Song song: mSongs) {
        List<Song> artistSongs = byArtist.get(song.getmArtists());
        if(artistSongs == null) {
            artistSongs = new ArrayList<>();
            byArtist.put(song.getmArtists(), artistSongs);
        } 
    artistSongs.add(song);

    }
    return byArtist; 
} 

public Set<String> getArtists() {
    return byArtist().keySet();
} 

public List<Song> getSongsForArtist(String artistName) {

    List <Song> songs = byArtist().get(artistName);
    songs.sort(new Comparator<Song>() {                                // HERE IS THE PROBLEM! 
        @Override

        public int compare (Song song1, Song song2){
            if(song1.equals(song2)) {
                return 0;
            } 
    return song1.mTitle.compareTo(song2.mTitle);    
                                    }
                                        });
     return songs;
                                    }

}

Walter Allen
Walter Allen
iOS Development with Swift Techdegree Student 16,021 Points

Hey, Mojo! I though I had responded to this yesterday. My bad. I'm so sorry.

At any rate, I've been working through the code, and I realized that the List<Song> class does not currently have a sort() method. (It's possible that this is in one of the helper files that comes with the lesson, but I'm not sure there.

That said, I was able to get the code working by replacing the call to sort() from a List<Song> instance with a call to sort() from the Collections class as a static method. To do this, you would need to replace

    songs.sort(new Comparator<Song>() {                                // HERE IS THE PROBLEM! 
        @Override

        public int compare (Song song1, Song song2){
            if(song1.equals(song2)) {
                return 0;
            } 
    return song1.mTitle.compareTo(song2.mTitle);    
                                    }
                                        });

with

Collections.sort(songs, new Comparator<Song>() {
        @Override
        public int compare (Song song1, Song song2){
            if(song1.equals(song2)) {
                return 0;
            } 
        return song1.mTitle.compareTo(song2.mTitle);    
        }
});

Make sure that you have imported java.util.Collections as well.

Does that help any? (Again, sorry for the late reply. :)

Mojo Waters
Mojo Waters
5,748 Points

Hi Walter,

Thanks a lot man. I understand your solution, but with my level of experience I'd be never able to add Collections. sort. I mean when I click sort method (List<T>, Comparator(?super T>) <T>: void ) so that makes sense that it is working, but just picking Collections out of nowhere - I mean, cool.

Don't apologize, I am thankful for your help.

Are you on fb or any social site? I am Martin Waters on fb add me if you want.

Thanks again man, and keep rocking out there Martin

I also had this problem, Craig's code worked on the treehouse side, but not on eclipse neon. The indicated fix worked both places.