Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting files based on its creation date in android

Tags:

file

android

I want list of file based on my creation date. When i updating any if images and trying to retrive all images,then orders are changed randomly.

Here is my code,

 File[] files = parentDir.listFiles();
    for (File file : files) {
// I am getting files here
    }

Any help..

like image 449
Jatin Avatar asked Oct 27 '14 06:10

Jatin


People also ask

Can you sort files by date?

Click the sort option in the top right of the Files area and select Date from the dropdown. Once you have Date selected, you will see an option to switch between descending and ascending order.


2 Answers

    List<File> fileList = new ArrayList<File>();
    Collections.sort(fileList, new Comparator<File>() {

        @Override
        public int compare(File file1, File file2) {
            long k = file1.lastModified() - file2.lastModified();
            if(k > 0){
               return 1;
            }else if(k == 0){
               return 0;
            }else{
              return -1;
           }
        }
    });
like image 181
Jclick Avatar answered Sep 23 '22 05:09

Jclick


I want list of file based on my creation date.

As the two previous answers pointed out, you can sort the files according to the modification date:

file.lastModified()

But the modification date is updated e.g. in the instant of renaming a file. So, this won't work to represent the creation date.

Unfortunately, the creation date is not available, thus you need to rethink your basic strategy:

see an old answer of CommonsWare

like image 38
Hartmut Pfitzinger Avatar answered Sep 22 '22 05:09

Hartmut Pfitzinger