0

I am saving the images in an app-specific folder and when i am trying to fetch the list of all the images of that folder and saving them in an array, the order of images which i am getting is random. I want the list of images in the order they are created i.e the newest one should come on top. Is there any function to achieve that. I am using this code to get the list of all files and storing them in an array.

 File[] listFile = downloadDir.listFiles();
Kunal
  • 412
  • 5
  • 21

1 Answers1

1

You can always sort this Array by calling the sort method with a comparator that will sort the files based on their creation date. A way this could be achieved is explained here: Best way to list files in Java, sorted by Date Modified?

Christophe Loeys
  • 196
  • 2
  • 11
  • i have one more doubt, the order of image is coming proper now, they are no longer random. But still the images are in reverse order i.e. the oldest image is coming first and the recent one is coming last. How i can change it so that i can get the latest image first? – Kunal Dec 20 '17 at 10:12
  • If you used the code from the post I linked you, I think you just have to reverse the order of the objects compared. So instead of `public int compare(File f1, File f2) { return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); }` you write `public int compare(File f1, File f2) { return Long.valueOf(f2.lastModified()).compareTo(f1.lastModified()); ` – Christophe Loeys Dec 20 '17 at 10:18