Wednesday, 11 July 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.

No comments:

Post a Comment