比较器<文件>对于“目录优先”命令

发布于 2024-10-07 09:52:25 字数 1483 浏览 0 评论 0原文

我很困惑......假设我有这个目录树:

{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笔芯 2024-10-14 09:52:25

这在我的测试中有效。

new Comparator<File>() {
    @Override
    public int compare(File first, File second) {
        if (first.isDirectory() && second.isDirectory())
            return first.compareTo(second);

        if (first.isDirectory())
            return this.compareToFile(first, second);

        if (second.isDirectory())
            return -(this.compareToFile(second, first));

        return this.compareFiles(first, second);
    }

    private int compareFiles(File first, File second) {
        File firstParentFile = first.getParentFile();
        File secondParentFile = second.getParentFile();

        if (isSubDir(firstParentFile, secondParentFile))
            return -1;

        if (isSubDir(secondParentFile, firstParentFile))
            return 1;

        return first.compareTo(second);
    }

    private int compareToFile(File directory, File file) {
        File fileParent = file.getParentFile();
        if (directory.equals(fileParent))
            return -1;

        if (isSubDir(directory, fileParent))
            return -1;

        return directory.compareTo(file);
    }

    private boolean isSubDir(File directory, File subDir) {
        for (File parentDir = directory.getParentFile(); parentDir != null; parentDir = parentDir.getParentFile()) {
            if (subDir.equals(parentDir)) {
                return true;
            }
        }

        return false;
    }

This works in my tests.

new Comparator<File>() {
    @Override
    public int compare(File first, File second) {
        if (first.isDirectory() && second.isDirectory())
            return first.compareTo(second);

        if (first.isDirectory())
            return this.compareToFile(first, second);

        if (second.isDirectory())
            return -(this.compareToFile(second, first));

        return this.compareFiles(first, second);
    }

    private int compareFiles(File first, File second) {
        File firstParentFile = first.getParentFile();
        File secondParentFile = second.getParentFile();

        if (isSubDir(firstParentFile, secondParentFile))
            return -1;

        if (isSubDir(secondParentFile, firstParentFile))
            return 1;

        return first.compareTo(second);
    }

    private int compareToFile(File directory, File file) {
        File fileParent = file.getParentFile();
        if (directory.equals(fileParent))
            return -1;

        if (isSubDir(directory, fileParent))
            return -1;

        return directory.compareTo(file);
    }

    private boolean isSubDir(File directory, File subDir) {
        for (File parentDir = directory.getParentFile(); parentDir != null; parentDir = parentDir.getParentFile()) {
            if (subDir.equals(parentDir)) {
                return true;
            }
        }

        return false;
    }
离鸿 2024-10-14 09:52:25

这个递归得到排序的文件树,如你所愿:

public static void main(String[] args) {
    List<File> files = getFileTree(new File("."));

    for (File f : files)
        System.out.println(f);
}

private static List<File> getFileTree(File file) {

    List<File> files = new LinkedList<File>();
    files.add(file);

    if (file.isDirectory()) {
        File[] current = file.listFiles();
        Arrays.sort(current, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory())
                    return o2.isDirectory() ? o1.compareTo(o2) : -1;
                else if (o2.isDirectory())
                    return 1;

                return o1.compareTo(o2);
            }
        });

        for (File f : current)
            files.addAll(getFileTree(f));
    }

    return files;
}

This recursivly gets the sorted file tree as you like:

public static void main(String[] args) {
    List<File> files = getFileTree(new File("."));

    for (File f : files)
        System.out.println(f);
}

private static List<File> getFileTree(File file) {

    List<File> files = new LinkedList<File>();
    files.add(file);

    if (file.isDirectory()) {
        File[] current = file.listFiles();
        Arrays.sort(current, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory())
                    return o2.isDirectory() ? o1.compareTo(o2) : -1;
                else if (o2.isDirectory())
                    return 1;

                return o1.compareTo(o2);
            }
        });

        for (File f : current)
            files.addAll(getFileTree(f));
    }

    return files;
}
毅然前行 2024-10-14 09:52:25
Comparator<File> fc = new Comparator<File>(){
        @Override public int compare(File o1, File o2) {
            boolean isFirstDirectory = o1.isDirectory();
            boolean isSecondDirectory = o2.isDirectory();

            if(!isFirstDirectory && !isSecondDirectory) {
                return o1.compareTo(o2);

            } else if (isFirstDirectory && !isSecondDirectory){
                return (int) 1;
            } else if(isSecondDirectory && !isFirstDirectory) {
                return -1;
            } else {
                return o1.compareTo(o2);
            }


        }
    };
Comparator<File> fc = new Comparator<File>(){
        @Override public int compare(File o1, File o2) {
            boolean isFirstDirectory = o1.isDirectory();
            boolean isSecondDirectory = o2.isDirectory();

            if(!isFirstDirectory && !isSecondDirectory) {
                return o1.compareTo(o2);

            } else if (isFirstDirectory && !isSecondDirectory){
                return (int) 1;
            } else if(isSecondDirectory && !isFirstDirectory) {
                return -1;
            } else {
                return o1.compareTo(o2);
            }


        }
    };
栖竹 2024-10-14 09:52:25

编辑
您必须比较第一级的基本目录和第二级的文件名

import java.io.File;
import java.io.IOException;
import java.util.Comparator;

public class FileComparator implements Comparator<File> {

    @Override
    public int compare(File o1, File o2) {

        int pathCompare = this.getPath(o1).compareTo(this.getPath(o2));
        if (pathCompare != 0) {
            return subPathCompare;
        } else {
            if (o1.isDirectory() && !o2.isDirectory()) {
                return -1;
            } else if (!o1.isDirectory() && o2.isDirectory()) {
                return 1;
            } else {
                return o1.compareTo(o2);
            }
        }
    }

    //maybe there is a better way to speparete path and file name, but it works
    private String getPath(File file) {
        if (file.isFile()) {
            return file.getParent().toLowerCase();
        } else {
            return file.getPath().toLowerCase();
        }
    }
}

如果路径和文件的分离是必要的,因为如果只这样做:

        if (o1.isDirectory() && !o2.isDirectory()) {
            return -1;
        } else if (!o1.isDirectory() && o2.isDirectory()) {
            return 1;
        } else {
            return o1.compareTo(o2);
        }

结果将不是情况 1,而是所有目录都将是的另一种情况第一项,文件是最后一项:

  \dir1\
  \dir2\
  \dir3\
  \dir4\
  \dir1\file1\
  \dir1\file2\
  \dir2\file1\
  \dir2\file2\

所以我的解决方案是首先检查两个比较的文件是否属于同一目录。

  • 如果不属于同一目录,则按该目录进行比较。
  • 如果属于同一目录,则子目录将“排序”在普通文件“之前”。

Edit
You have to compare the Base Directory at first level and File name at second level

import java.io.File;
import java.io.IOException;
import java.util.Comparator;

public class FileComparator implements Comparator<File> {

    @Override
    public int compare(File o1, File o2) {

        int pathCompare = this.getPath(o1).compareTo(this.getPath(o2));
        if (pathCompare != 0) {
            return subPathCompare;
        } else {
            if (o1.isDirectory() && !o2.isDirectory()) {
                return -1;
            } else if (!o1.isDirectory() && o2.isDirectory()) {
                return 1;
            } else {
                return o1.compareTo(o2);
            }
        }
    }

    //maybe there is a better way to speparete path and file name, but it works
    private String getPath(File file) {
        if (file.isFile()) {
            return file.getParent().toLowerCase();
        } else {
            return file.getPath().toLowerCase();
        }
    }
}

If the separation of path and file is nessesary, because if one only do:

        if (o1.isDirectory() && !o2.isDirectory()) {
            return -1;
        } else if (!o1.isDirectory() && o2.isDirectory()) {
            return 1;
        } else {
            return o1.compareTo(o2);
        }

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:

  \dir1\
  \dir2\
  \dir3\
  \dir4\
  \dir1\file1\
  \dir1\file2\
  \dir2\file1\
  \dir2\file2\

So my solution is to check first if the two compared files belong to the same directory.

  • If they do not belong to the same directory, so they are compared by there directory.
  • If the belong to the same directory, then the sub directorys are "sorted" "before" the normal files.
你曾走过我的故事 2024-10-14 09:52:25

我确实找到了一种实现与我想要的伪相反的方法(文件之后的目录,没有检查文件和目录名称位于根级别的边缘情况)。

...不,将其转换为我想要的并不简单,因为我使用目录的字典顺序,它将不同级别的目录排序和同一级别的目录排序结合在一起。

static class FileDirectoryPriorityComparator implements Comparator<File>
{
    @Override public int compare(File f1, File f2) {
        if (f1.isDirectory())
        {
            if (f2.isDirectory())
            {
                return f1.compareTo(f2);                        
            }
            else
            {
                return compareDirFile(f1, f2);
            }
        }
        else
        {
            if (f2.isDirectory())
            {
                return -compareDirFile(f2, f1);
            }
            // f1 and f2 are both files
            else
            {
                return compareFiles(f1, f2);
            }
        }
    }

    private int compareDirFile(File dir, File file) {
        /* 
         * If dir compares differently to file's parent, use that ordering.
         * Otherwise, dir comes before file. 
         */              
        File fparent = file.getParentFile();

        if (fparent == null)
            return -1;

        int i = dir.compareTo(fparent);
        if (i != 0)
            return i;

        return -1;
    }

    private int compareFiles(File f1, File f2) {
        /* 
         * If f1's parent compares differently to f2's parent, use that ordering.
         * Otherwise use default ordering.
         */              
        File fp1 = f1.getParentFile();
        File fp2 = f2.getParentFile();

        if (fp1 == null)
        {
            if (fp2 != null)
            {
                return 1;
            }
        }
        else 
        {
            if (fp2 == null)
            {
                return -1;
            }
            else
            {
                int i = fp1.compareTo(fp2);
                if (i != 0)
                    return i;                   
            }
        }           

        return f1.compareTo(f2);
    }
}

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.

static class FileDirectoryPriorityComparator implements Comparator<File>
{
    @Override public int compare(File f1, File f2) {
        if (f1.isDirectory())
        {
            if (f2.isDirectory())
            {
                return f1.compareTo(f2);                        
            }
            else
            {
                return compareDirFile(f1, f2);
            }
        }
        else
        {
            if (f2.isDirectory())
            {
                return -compareDirFile(f2, f1);
            }
            // f1 and f2 are both files
            else
            {
                return compareFiles(f1, f2);
            }
        }
    }

    private int compareDirFile(File dir, File file) {
        /* 
         * If dir compares differently to file's parent, use that ordering.
         * Otherwise, dir comes before file. 
         */              
        File fparent = file.getParentFile();

        if (fparent == null)
            return -1;

        int i = dir.compareTo(fparent);
        if (i != 0)
            return i;

        return -1;
    }

    private int compareFiles(File f1, File f2) {
        /* 
         * If f1's parent compares differently to f2's parent, use that ordering.
         * Otherwise use default ordering.
         */              
        File fp1 = f1.getParentFile();
        File fp2 = f2.getParentFile();

        if (fp1 == null)
        {
            if (fp2 != null)
            {
                return 1;
            }
        }
        else 
        {
            if (fp2 == null)
            {
                return -1;
            }
            else
            {
                int i = fp1.compareTo(fp2);
                if (i != 0)
                    return i;                   
            }
        }           

        return f1.compareTo(f2);
    }
}
流殇 2024-10-14 09:52:25

当然太晚了。无论如何,这里都有一个 Comparator 实现,它按照此 SO 中的要求进行排序。

public class DirectoryBeforeFileComparator implements Comparator<File> {
    @Override
    public int compare(File o1, File o2) {
        if (o1.isDirectory() && !o2.isDirectory()) {
            // directory before non-directory.
            return -1;
        }
        if (!o1.isDirectory() && o2.isDirectory()) {
            // non-directory after directory
            return 1;
        }
        // compare two pathnames lexicographically
        return o1.compareTo(o2);
    }
}

Of course too late. In any case here is a Comparator implementation that sorts as asked for in this SO.

public class DirectoryBeforeFileComparator implements Comparator<File> {
    @Override
    public int compare(File o1, File o2) {
        if (o1.isDirectory() && !o2.isDirectory()) {
            // directory before non-directory.
            return -1;
        }
        if (!o1.isDirectory() && o2.isDirectory()) {
            // non-directory after directory
            return 1;
        }
        // compare two pathnames lexicographically
        return o1.compareTo(o2);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文