相关疑难解决方法(0)

如何排序List <File>首先列出目录并按目录分组文件?

为了获得包含在一个指定的目录,并根据一些扩展的所有文件,我使用的方法listFiles类的FileUtils阿帕奇百科全书IO库,如下面的代码样本.

ArrayList<String> wildcards = new ArrayList<>();
wildcards.add("*.cpp");
wildcards.add("*.h");
wildcards.add("*.txt");

File dir = new File("/path/to/dir");
Collection<File> found = FileUtils.listFiles(
        dir,
        new WildcardFileFilter(wildcards, IOCase.SENSITIVE),
        DirectoryFileFilter.DIRECTORY);

List<File> files = new ArrayList<>(found);
Run Code Online (Sandbox Code Playgroud)

结果中的项目顺序Collection<File>在不同的操作系统中有所不同,因此我会files根据以下规则对它们(即包装列表)进行排序.

  • 应在文件之前列出目录.
  • 排序例程应按目录分组文件.

例:

/path/to/dir/first/subpath/main.cpp
/path/to/dir/first/subpath/utils.cpp
/path/to/dir/first/subpath/utils.h
/path/to/dir/first/main.cpp
/path/to/dir/first/utils.cpp
/path/to/dir/first/utils.h
/path/to/dir/second/main.cpp
/path/to/dir/second/utils.cpp
/path/to/dir/second/utils.h
/path/to/dir/README.txt
Run Code Online (Sandbox Code Playgroud)

java sorting algorithm apache-commons comparator

8
推荐指数
1
解决办法
1421
查看次数

标签 统计

algorithm ×1

apache-commons ×1

comparator ×1

java ×1

sorting ×1