安卓開發(fā)之文件夾以及文件排序

字號:


    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());// 最后比較文件
    }
    }
    }
    }
    };