Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Local Development Environments Exploring Your IDE Running Your Code

Brian Bartholomew
Brian Bartholomew
1,741 Points

List.sort() does not exist in Java 7, what other methods could be used?

I am learning java for work. We have legacy software and use Java 7. List.sort() was introduced in Java 8. Hence, my compiling error:

Error:(81, 10) java: cannot find symbol
  symbol:   method sort(<anonymous java.util.Comparator<com.teamtreehouse.model.Song>>)
  location: variable songs of type java.util.List<com.teamtreehouse.model.Song>

I was thinking that refactoring the List to be a Tree of some sort would help the alphabetizing.

Is there a simpler way?

2 Answers

I fixed this using

        List<Song> songs = byArtist().get(artistName);
        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);
            }
        });

instead of

songs.sort(new Comparator<Song>() {


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

For earlier versions of Java I think you can use Collections in the java.util package instead.

Collections.sort( myComparableList );

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html