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

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

    I was getting an exception

    IllegalArgumentException: Comparison method violates its general contract!

    so I used this and it worked ok:

    Arrays.sort(filesList, new Comparator() {
       @Override
       public int compare(File a, File b) {
          if(a.lastModified() < b.lastModified() )
             return 1;
          if(a.lastModified() > b.lastModified() )
             return -1;
          return 0;
    }});
    

提交回复
热议问题