{{announcement.body}}
{{announcement.title}}

Road to Great Code, Challenge #2 — Sort Playlist

DZone 's Guide to

Road to Great Code, Challenge #2 — Sort Playlist

Explore a new coding challenge, circa 2001 — sorting an iPod playlist!

· Java Zone ·
Free Resource

Last week, in the Road to Great Code, we implemented a playlist for shuffling a playlist. This week, we are still stuck in 2001 and the iPod release date is approaching rather quickly. However, no one — until now — noticed that songs on the device are not sorted alphabetically. You are tasked with this last-minute task, and you best get it right. You don’t want to get on Steve’s bad side. He promised to personally test this feature himself, so the pressure is on.

You may also like: Best of DZone: Java Tutorials for Beginners, Experts, and Everyone In-Between

The Problem

Here’s the starting point of the program that you must complete. Naturally, I’ve populated the playlist with all of the festive songs I could think of because you can’t really get enough of them… or can you?

Java




x
27


1
public static void main(String[] args) {
2
        List<Song> library = new ArrayList<>() {
3
            {
4
                add(new Song("Santa Baby"));
5
                add(new Song("Do You Hear What I Hear"));
6
                add(new Song("Drummer Boy"));
7
                add(new Song("Santa Tell Me"));
8
                add(new Song("Silver Bells"));
9
                add(new Song("Last Christmas"));
10
                add(new Song("The 12 Days of Christmas"));
11
                add(new Song("All I Want For Christmas Is You"));
12
                add(new Song("Blue Christmas"));
13
                add(new Song("Jingle Bell Rock"));
14
                add(new Song("Sleigh Ride"));
15
                add(new Song("Run Rudolph Run"));
16
            }
17
        };
18
 
          
19
        mySort(library);
20
 
          
21
        library.forEach(System.out::println);
22
    }
23
 
          
24
    private static void mySort(List<Song> songs) {
25
        // type solution here
26
 
          
27
    }



The Task

Implement the mySort method so that it reorders the elements in the list in alphabetical order.

A Solution

This is my solution. Did you come up with something better? Make sure to put your answer in the comments section. My solution is only one line, but do you think it’s maintainable? Let me know. 

Java




xxxxxxxxxx
1


1
  private static void mySort(List<Song> songs) {
2
        // type solution here
3
        songs.sort(Comparator.comparing(song -> song.getName().toLowerCase()));
4
 
          
5
    }



Sorted Playlist Output

Further Reading

Best of DZone: Java Tutorials for Beginners, Experts, and Everyone In-Between

Road to Great Code, Challenge #1 — Shuffle Playlist 

How to Sort Objects in Java

Topics:
java ,java arraylist ,list ,sort ,comparator ,challenge ,coding challenges

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}