您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 安卓开发 >> 正文

安卓开发之文件夹以及文件排序

作者:佚名    责任编辑:admin    更新时间:2022-06-22

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

  }

  }

  }

  }

  };