| 范文 | static Comparator<File> comparator = new Comparator<File>() {
 public int compare(File f1, File f2) {
 if (f1 == null || f2 == null) {// 先比较null
 if (f1 == null) {
 {
 return -1;
 }
 } else {
 return 1;
 }
 } else {
 if (f1.isDirectory() == true && f2.isDirectory() == true) { // 再比较文件夹
 return f1.getName().compareToIgnoreCase(f2.getName());
 } else {
 if ((f1.isDirectory() && !f2.isDirectory()) == true) {
 return -1;
 } else if ((f2.isDirectory() && !f1.isDirectory()) == true) {
 return 1;
 } else {
 return f1.getName().compareToIgnoreCase(f2.getName());// 最后比较文件
 }
 }
 }
 }
 };
 |