How can I sort my arraist alphabetically? (Java)

I’m kind of new in java, and I don’t have much knowledge about coding yet, but maybe you can help me with my problem. I have a program that should display all the songs in alphabetical order. I want to work with collection APIs. But in my code I do not understand. Eclipse does not give me an error and it works on the emulator without sorting it correctly. Maybe you can show me pass my code what I need to do to make it work. Sorry for not being as good as you;) ... But I'm young and I want to learn all about coding. thanks a lot, Vinzenz :)

public class SongsManager {

final String MEDIA_PATH = Environment.getExternalStorageDirectory()
        .getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
private String mp4Pattern = ".mp4";
private String MP3Pattern = ".MP3";
private String MP4Pattern = ".MP4";
private String m4aPattern = ".m4a";

// Constructor
public SongsManager() {

}

/**
 * Function to read all mp3 files and store the details in
 * ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList() {
    System.out.println(MEDIA_PATH);
    if (MEDIA_PATH != null) {
        File home = new File(MEDIA_PATH);
        File[] listFiles = home.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                System.out.println(file.getAbsolutePath());
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);
                }
            }
        }

    }
    // return songs list array
    return songsList;



}

private void scanDirectory(File directory) {
    if (directory != null) {
        File[] listFiles = directory.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);

                }

            }
        }
    }
}

private void addSongToList(File song) {
    if (song.getName().endsWith(mp3Pattern) || song.getName().endsWith(mp4Pattern) || song.getName().endsWith(MP4Pattern) || song.getName().endsWith(MP3Pattern) || song.getName().endsWith(m4aPattern)) {
        HashMap<String, String> songMap = new HashMap<String, String>();
        songMap.put("songTitle",
                song.getName().substring(0, (song.getName().length() - 4)));
        songMap.put("songPath", song.getPath());

        // Adding each song to SongList
        songsList.add(songMap);
    }
}

public static void main (String[] args) {
    List<String> songsList = new LinkedList <String>();
    System.out.println(songsList);
    Collections.sort(songsList);
    System.out.println(songsList);

}



}
+4
source share
1

Collections.sort(songsList) - , @Matt.

, songsList String 's, String Comparable.

+6

All Articles