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

后端 未结 9 729
轮回少年
轮回少年 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:33

    If you want to sort date wise then choose below code, and if you want name then already answer is above :)

    File f = new File("/home/myfiles");
    File [] files = f.listFiles();
    Arrays.sort( files, new Comparator(){
    public int compare(Object o1, Object o2) {
    
        if (((File)o1).lastModified() > ((File)o2).lastModified()) {
            return -1;
        } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
            return +1;
        } else {
            return 0;
        }
    }
    }); 
    

提交回复
热议问题