对要排序的集合 List<PhotoItem> list = new List<PhotoItem>
先说一个我这个Photoltem中和字段:private String path; private Long lastModified;
先把自己读到的图片都放到这个集合中,在读图片的时候给图片设上他的setLastModified
list.get(i).setLastModified(new File(list.get(i).getPath()).lastModified());
排序的时候只需:Collections.sort(list,new FileComparator());
附上FileComparator():
public class FileComparator implements Comparator<PhotoItem> {
public int compare(PhotoItem file1, PhotoItem file2) {
if (file1.getLastModified() > file2.getLastModified()) {
return -1;
} else {
return 1;
}
}
public FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
String tmp = file.getName().toLowerCase();
if (tmp.endsWith(".mov") || tmp.endsWith(".jpg")) {
return true;
}
return false;
}
};
}