How can I sort my Arraylist alphabetical? (Java)

16,826

Just use Collections.sort(songsList)--once you've populated the list as @Matt points out.

This will work because the items in songsList are String's, and String implements Comparable.

Share:
16,826
vinzzenzz
Author by

vinzzenzz

Updated on June 08, 2022

Comments

  • vinzzenzz
    vinzzenzz almost 2 years

    I am kind of new in java and I have not already a big knowledge about coding but maybe you can help me with my problem. I have a program which should list all the songs in a alphabetical right order. I want to work with the Collections API. But in my code I don't get it right. Eclipse doesnt give me an error and on the emulator it runs, without to sort it right. Maybe you can show me, transferred to my code what I have to do, to get it running. Sorry for being not as good as you ;) ...But I am 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);
    
    }
    
    
    
    }
    
  • Matt Ball
    Matt Ball over 10 years
    You might notice that the OP is already trying that... just not correctly.
  • TheSuccessor
    TheSuccessor over 10 years
    The items in songsList are actually HashMap<String, String>s
  • Vidya
    Vidya over 10 years
    Good call, Matt. Edited accordingly.
  • vinzzenzz
    vinzzenzz over 10 years
    Can you please show me exactly what I have to do in my code now? I dont understand it
  • Vidya
    Vidya over 10 years
    First, Android does not have a main method entry point as Matt points out, but that's outside the scope of the question. But you need to construct songsList, populate it with multiple calls to songsList.add(songTitle), and then call Collections.sort(songsList). Right now you are sorting an empty list.