比较器<文件>对于“目录优先”命令文件>
我很困惑......假设我有这个目录树:
{someRoot}/
{someRoot}/bar/
{someRoot}/bar/file1.txt
{someRoot}/foo/
{someRoot}/foo/baz/
{someRoot}/foo/baz/file3.txt
{someRoot}/foo/abracadabra.txt
{someRoot}/foo/file2.txt
{someRoot}/aardvark.txt
{someRoot}/food.txt
{someRoot}/zebra.txt
您会注意到顺序。将此称为订单1。在每个阶段,目录先于文件。 (注意: bar/file1.txt
位于 foo
之前,因此在全局范围内,目录并不全部位于所有文件之前。 )
如果我枚举此目录树,然后递归枚举子目录,我将得到以下 List
,排序为 order2。
{someRoot}/
{someRoot}/aardvark.txt
{someRoot}/bar/
{someRoot}/foo/
{someRoot}/food.txt
{someRoot}/zebra.txt
{someRoot}/bar/file1.txt
{someRoot}/foo/abracadabra.txt
{someRoot}/foo/baz/
{someRoot}/foo/file2.txt
{someRoot}/foo/baz/file3.txt
如果我创建简单的 Comparator
:
Comparator<File> fc = new Comparator<File>(){
@Override public int compare(File o1, File o2) {
return o1.compareTo(o2);
}
};
并进行排序,我会从字典顺序中得到这个排序 (order3):
{someRoot}
{someRoot}/aardvark.txt
{someRoot}/bar
{someRoot}/bar/file1.txt
{someRoot}/foo
{someRoot}/food.txt
{someRoot}/foo/abracadabra.txt
{someRoot}/foo/baz
{someRoot}/foo/baz/file3.txt
{someRoot}/foo/file2.txt
{someRoot}/zebra.txt
但我不想要这个排序(它有问题:请注意 food.txt
位于目录 foo
及其子项之间),我想要 order1。我怎样才能写一个比较器来实现这一点?
I'm stumped... Let's say I have this directory tree:
{someRoot}/
{someRoot}/bar/
{someRoot}/bar/file1.txt
{someRoot}/foo/
{someRoot}/foo/baz/
{someRoot}/foo/baz/file3.txt
{someRoot}/foo/abracadabra.txt
{someRoot}/foo/file2.txt
{someRoot}/aardvark.txt
{someRoot}/food.txt
{someRoot}/zebra.txt
You'll note the ordering. Call this order1. At each stage, the directories come first before the files. (NOTE: bar/file1.txt
comes before foo
, so on a global basis, the directories do not all come before all the files.)
If I enumerate this directory tree, and then recursively enumerate the subdirectories, I'll get the following List<File>
, with ordering order2.
{someRoot}/
{someRoot}/aardvark.txt
{someRoot}/bar/
{someRoot}/foo/
{someRoot}/food.txt
{someRoot}/zebra.txt
{someRoot}/bar/file1.txt
{someRoot}/foo/abracadabra.txt
{someRoot}/foo/baz/
{someRoot}/foo/file2.txt
{someRoot}/foo/baz/file3.txt
If I create the straightforward Comparator<File>
:
Comparator<File> fc = new Comparator<File>(){
@Override public int compare(File o1, File o2) {
return o1.compareTo(o2);
}
};
and I sort, I get this ordering (order3) from lexicographic ordering:
{someRoot}
{someRoot}/aardvark.txt
{someRoot}/bar
{someRoot}/bar/file1.txt
{someRoot}/foo
{someRoot}/food.txt
{someRoot}/foo/abracadabra.txt
{someRoot}/foo/baz
{someRoot}/foo/baz/file3.txt
{someRoot}/foo/file2.txt
{someRoot}/zebra.txt
But I don't want this ordering (which has problems: note that food.txt
comes between directory foo
and its sub-items), I want order1. How can I write a Comparator to get me that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这在我的测试中有效。
This works in my tests.
这个递归得到排序的文件树,如你所愿:
This recursivly gets the sorted file tree as you like:
编辑
您必须比较第一级的基本目录和第二级的文件名
如果路径和文件的分离是必要的,因为如果只这样做:
结果将不是情况 1,而是所有目录都将是的另一种情况第一项,文件是最后一项:
所以我的解决方案是首先检查两个比较的文件是否属于同一目录。
Edit
You have to compare the Base Directory at first level and File name at second level
If the separation of path and file is nessesary, because if one only do:
the the result would be not case 1, but an other case where all directorys would be the first items, and the files are the last:
So my solution is to check first if the two compared files belong to the same directory.
我确实找到了一种实现与我想要的伪相反的方法(文件之后的目录,没有检查文件和目录名称位于根级别的边缘情况)。
...不,将其转换为我想要的并不简单,因为我使用目录的字典顺序,它将不同级别的目录排序和同一级别的目录排序结合在一起。
I did find a way to implement the pseudo-opposite of what I wanted (directories after files, haven't checked the edge cases where files and directory names are at the root level).
...And no, it's not trivial to transform this into what I do want, because I'm using lexicographic ordering of directories, which couples the ordering of directories at different levels and the ordering of directories at the same level.
当然太晚了。无论如何,这里都有一个 Comparator 实现,它按照此 SO 中的要求进行排序。
Of course too late. In any case here is a
Comparator
implementation that sorts as asked for in this SO.