如何按字母顺序对我的数组列表进行排序

编程入门 行业动态 更新时间:2024-10-20 20:51:17
本文介绍了如何按字母顺序对我的数组列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想按字母顺序对我的数组列表进行排序,但我不明白...也许你可以帮助我.我不知道为什么,但我不能使用 collections.sort...我编码错了什么?非常感谢你的帮助和时间,Vinzenz

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);
        }
    }


}

推荐答案

使用 Collections.sort

Collections.sort(listOfStrings, String.CASE_INSENSITIVE_ORDER);

详情请参阅以下内容:

String.CASE_INSENSITIVE_ORDER

Collections.sort

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

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

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

更多推荐

[db:关键词]

本文发布于:2023-04-16 10:15:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/884969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字母   顺序   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!