我如何可以排序的ArrayList我按字母顺序排列 [英] How can i sort my arraylist alphabetical

查看:167
本文介绍了我如何可以排序的ArrayList我按字母顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的排序按字母顺序排列的ArrayList,但我不明白这一点...也许你能帮助我。我不知道为什么,但我不能与collections.sort工作...我是什么编码错了吗?
 非常感谢您的帮助和时间,Vinzenz

 公共类SongsManager {    最后弦乐MEDIA_PATH = Environment.getExternalStorageDirectory()
            .getPath()+/;
    私人的ArrayList<&HashMap的LT;字符串,字符串>> songsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
    私人字符串mp3Pattern =.MP3
    私人字符串mp4Pattern =.MP4
    私人字符串MP3Pattern =.MP3
    私人字符串MP4Pattern =.MP4
    私人字符串m4aPattern =名为.m4a;    //构造
    公共SongsManager(){    }    / **
     *功能读取所有MP3文件,并存储在细节
     * ArrayList的
     * * /
    公众的ArrayList<&HashMap的LT;字符串,字符串>> getPlayList(){
        的System.out.println(MEDIA_PATH);
        如果(MEDIA_PATH!= NULL){
            文件首页=新的文件(MEDIA_PATH);
            文件[] listFiles = home.listFiles();
            如果(listFiles =空&放大器;!&放大器; listFiles.length大于0){
                对于(文件文件:listFiles){
                    的System.out.println(file.getAbsolutePath());
                    如果(file.isDirectory()){
                        scanDirectory(文件);
                    }其他{
                        addSongToList(文件);
                    }
                }
            }        }
        //返回歌曲列表数组
        返回songsList;    }    私人无效scanDirectory(文件目录){
        如果(目录!= NULL){
            文件[] listFiles = directory.listFiles();
            如果(listFiles =空&放大器;!&放大器; listFiles.length大于0){
                对于(文件文件:listFiles){
                    如果(file.isDirectory()){
                        scanDirectory(文件);
                    }其他{
                        addSongToList(文件);                    }                }
            }
        }
    }    私人无效addSongToList(文件歌曲){
        如果(song.getName()的endsWith(mp3Pattern)|| song.getName()的endsWith(mp4Pattern)|| song.getName()的endsWith(MP4Pattern)|| song.getName()的endsWith(MP3Pattern)|| song.getName()。的endsWith(m4aPattern)){
            HashMap的<字符串,字符串> songMap =新的HashMap<字符串,字符串>();
            songMap.put(SONGTITLE
                    。song.getName()子串(0,(song.getName()长度() - 4)));
            songMap.put(songPath,song.getPath());            //添加每一首歌的SongList
            songsList.add(songMap);
        }
    }
}


解决方案

使用 Col​​lections.sort

  Col​​lections.sort(listOfStrings,String.CASE_INSENSITIVE_ORDER);

详情请参阅以下内容:

String.CASE_INSENSITIVE_ORDER

<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29\"相对=nofollow> Col​​lections.sort

您还可以创建自定义比较,而不是使用 String.CASE_INSENSITIVE_ORDER

I want to sort my arraylist alphabetical but I don't get it...maybe you can help me. I dont know why but i cant work with collections.sort...what am i coding wrong? thanks a lot for all your help and time, 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(listOfStrings, String.CASE_INSENSITIVE_ORDER);

See the following for details:

String.CASE_INSENSITIVE_ORDER

Collections.sort

You can also create a custom Comparator, instead of using String.CASE_INSENSITIVE_ORDER

这篇关于我如何可以排序的ArrayList我按字母顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆