Wednesday, July 11, 2012

Sort directory/file in order of lastModified time

Last exercise describe how to Sort directory/file in alphabetical order ignore case. To Sort directory/file in order of lastModified time, replace filecomparator() method of last exercise with following code:

    Comparator<? super File> filecomparatorByLastModified = new Comparator<File>(){
  
  public int compare(File file1, File file2) {

   if(file1.isDirectory()){
    if (file2.isDirectory()){
     return Long.valueOf(file1.lastModified()).compareTo(file2.lastModified());
    }else{
     return -1;
    }
   }else {
    if (file2.isDirectory()){
     return 1;
    }else{
     return Long.valueOf(file1.lastModified()).compareTo(file2.lastModified());
    }
   }
    
  }  
 };


Download the files.

Next:
- File Explorer with sorting, in alphabetical and lastModified.


8 comments:

Jayesh Sorathia said...

Very good post.

Rasam Hossain said...

How can I add two buttons so that they can work for those two different sorting order. suppose I will add one button that will sort alphabetically and the other button will sort in lastDateModified. I tried but failed to do this. Can you please help? And how can I use cut, copy and paste methods so that I can cut, copy or paste any file from one directory to another directory?

Andr.oid Eric said...

hello Rasam Hossain,

Please read File Explorer with sorting, in alphabetical and lastModified..

Matthew said...

I am having a problem running this application in Eclipse. It says it has installed fine, but when I click on the launcher icon it just said "Unfortunately, AndroidExplorer has stopped." Do you have any idea why this could be happening?

Andr.oid Eric said...

Hello Matthew,

I don't know what your exact case.

What min SDK level you set? please notice that start from Android 4.1, API Level: 16, you have to set permission of READ_EXTERNAL_STORAGE to read SC Card. New permission for Android 4.1, API Level: 16..

Matthew said...

Thank you for your help. I am running Android 4.1 API Level 16. I tried adding this code

""

to my Manifest file, and that did not resolve the problem. I am really new to android programming, so I am sorry if that was a completely wrong action to take.

Also, is there an easy way to populate the directory we are accessing with files for the sake development?

Thank you for all of your help!

Matthew said...

Sorry the code was:

Matthew said...

also if it helps... it says in Logicat:

error opening trace file no such file or directory (2) android