android 对文件夹以及文件排序

来源:互联网 发布:好用的ss代理 知乎 编辑:程序博客网 时间:2024/06/05 18:01
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());// 最后比较文件
}
}
}
}
};