Android: how to get spelling list by name by date descending?

I can do it:

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

I copied the answer from stackoverflow!

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

+8
android file
source share
7 answers
 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().compareTo(object2.getName()); } }); } 

Use Array.sort() to compare the file name.

Edit: Use this code to sort by date

 final File[] sortedByDate = folder.listFiles(); if (sortedByDate != null && sortedByDate.length > 1) { Arrays.sort(sortedByDate, new Comparator<File>() { @Override public int compare(File object1, File object2) { return (int) ((object1.lastModified() > object2.lastModified()) ? object1.lastModified(): object2.lastModified()); } }); } 
+17
source share

Pointers:

 File.isDirectory() File.lastModified() 
+2
source share

The correct way to sort files by the date of the last change in reverse is as follows:

 Arrays.sort(Files, LastModifiedFileComparator.LASTMODIFIED_REVERSE); 

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

+1
source share

You can use the code below. worked for me.

 final File[] files= file.listFiles(); if (files != null && files.length > 1) { Collections.sort(Arrays.asList(files), new Comparator<File>() { public int compare(File o1, File o2) { long lastModifiedO1 = o1.lastModified(); long lastModifiedO2 = o2.lastModified(); return (lastModifiedO2 < lastModifiedO1) ? -1 : ((lastModifiedO1 > lastModifiedO2) ? 1 : 0); } }); } 
+1
source share

If you want to sort the date wise, then select the code below, and if you want the name to have an answer higher :)

 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; } } }); 
0
source share

I solved this problem using this

 final File[] sortedByDate = folder.listFiles(); if (sortedByDate != null && sortedByDate.length > 1) { Arrays.sort(sortedByDate, new Comparator<File>() { @Override public int compare(File object1, File object2) { return (int) ((object1.lastModified() > object2.lastModified()) ? 0: 1); } }); } 

I am returning 0 and 1 lastModified() value which solved my problem.

0
source share

I get an exception

IllegalArgumentException: comparison method violates the general contract!

so I used this and it worked fine:

 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; }}); 
0
source share

All Articles