Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

file

android

I'm able to do this:

    File images = new File(path);  
    File[] imageList = images.listFiles(new FilenameFilter(){  
        public boolean accept(File dir, String name)  
        {  
            return name.endsWith(".jpg");
        }  
    });

I copied from an answer of stackoverflow !

Is there a way to listFile ok kind "directory" (folder) and to list by reverse alphabetical order ? ... And by reverse date ?

like image 689
realtebo Avatar asked Sep 13 '12 15:09

realtebo


People also ask

How do I sort files by name in descending order?

To sort file names or other columns (such as Date Modified) in Windows Explorer or in any Office dialog based on Explorer (Open, Save As, etc.), click on the column's title. One click will sort in ascending order, and the next click will reverse it to descending order, and so back and forth. Was this reply helpful?


1 Answers

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<File>() {
   @Override
   public int compare(File a, File b) {
      if(a.lastModified() < b.lastModified() )
         return 1;
      if(a.lastModified() > b.lastModified() )
         return -1;
      return 0;
}});
like image 67
einschneidend Avatar answered Sep 23 '22 00:09

einschneidend