简体   繁体   English

我怎样才能按字母顺序排列我的arraylist

[英]How can i sort my arraylist alphabetical

I want to sort my arraylist alphabetical but I don't get it...maybe you can help me. 我想按照字母顺序排列我的arraylist,但我不明白......也许你可以帮助我。 I dont know why but i cant work with collections.sort...what am i coding wrong? 我不知道为什么,但我无法使用collections.sort ...我编码错误是什么? thanks a lot for all your help and time, Vinzenz 非常感谢你的帮助和时间,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);
        }
    }


}

Use Collections.sort 使用Collections.sort

Collections.sort(listOfStrings, String.CASE_INSENSITIVE_ORDER);

See the following for details: 有关详细信息,请参阅以下

String.CASE_INSENSITIVE_ORDER String.CASE_INSENSITIVE_ORDER

Collections.sort Collections.sort

You can also create a custom Comparator , instead of using String.CASE_INSENSITIVE_ORDER 您还可以创建自定义Comparator ,而不是使用String.CASE_INSENSITIVE_ORDER

您必须实现自己的Comparator<HashMap<String,String>>并将其传递给Collections.sort

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM