Android: how to get a directory list ordered by name or by date descending?

后端 未结 9 646
轮回少年
轮回少年 2020-12-30 09:48

I\'m able to do this:

    File images = new File(path);  
    File[] imageList = images.listFiles(new FilenameFilter(){  
        public boolean accept(File          


        
相关标签:
9条回答
  • 2020-12-30 10:36

    The right way to order files by date of last modified in reverse mode is doing:

    Arrays.sort(Files, LastModifiedFileComparator.LASTMODIFIED_REVERSE); 
    

    for this instruction you will need the commons.io library from org.apache that you can download from here

    0 讨论(0)
  • 2020-12-30 10:39

    For sort based on file name,Add

    object1.getName().toLowerCase(Locale.getDefault())
    

    instead of

    object1.getName()
    

    to avoid sort issues causing by Locale changes and upper/low case filenames

                 final File[] sortedFileName = images.listFiles();
    
                    if (sortedFileName != null && sortedFileName.length > 1) {
                        Arrays.sort(sortedFileName, new Comparator<File>() {
                            @Override
                            public int compare(File object1, File object2) {
                                return object1.getName().toLowerCase(Locale.getDefault()).compareTo(object2.getName().toLowerCase(Locale.getDefault()));
                            }
                        });
                    }
    
    0 讨论(0)
  • 2020-12-30 10:42

    If you prefer brevity:

    File[] files = folder.listFiles();
    Arrays.sort(files, (a, b) -> Long.compare(b.lastModified(), a.lastModified()));
    
    0 讨论(0)
提交回复
热议问题