File[] directoryList = currentFolder.listFiles();
if (directoryList != null) {
List<File> directoryListing = new ArrayList<File>();
directoryListing.addAll(Arrays.asList(directoryList));
Collections.sort(directoryListing, new SortFileName());
Collections.sort(directoryListing, new SortFolder());
}
//sorts based on the files name
public class SortFileName implements Comparator<File> {
@Override
public int compare(File f1, File f2) {
return f1.getName().compareTo(f2.getName());
}
}
//sorts based on a file or folder. folders will be listed first
public class SortFolder implements Comparator<File> {
@Override
public int compare(File f1, File f2) {
if ((f1.isDirectory() && f2.isDirectory())
|| (!f1.isDirectory() && !f2.isDirectory()))
return 0;
else if (f1.isDirectory() && !f2.isDirectory())
return -1;
else
return 1;
}
}
public class SortFileDate implements Comparator<File> {
@Override
public int compare(File f1, File f2) {
return Long.compare(f1.lastModified(), f2.lastModified());
}
}
沒有留言:
張貼留言