比较器<file>表示“目录优先”命令</file>

时间:2010-12-14 15:29:37

标签: java file comparator

我很难过......假设我有这个目录树:

{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

你会注意到订购。将此 order1 称为。在每个阶段,目录首先出现在文件之前。 (注意: bar/file1.txtfoo之前,所以在全球范围内,目录并非都在所有文件之前。)

如果我枚举这个目录树,然后递归枚举子目录,我将得到以下List<File>,订购 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<File>

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 。我怎样才能写一个比较器来帮助我呢?

6 个答案:

答案 0 :(得分:4)

这适用于我的测试。

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;
    }

答案 1 :(得分:2)

这个递归可以根据需要获取已排序的文件树:

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;
}

答案 2 :(得分:0)

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

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,而是另一个案例,其中所有directorys都是第一个项目,文件是最后一个:

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

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

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

答案 3 :(得分:0)

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);
            }


        }
    };

答案 4 :(得分:0)

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

......不,将它转换成我想要的东西并不容易,因为我正在使用目录的词典排序,它将不同级别的目录排序和同一级别的目录排序结合起来。

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);
    }
}

答案 5 :(得分:0)

当然为时已晚。在任何情况下,这里都有一个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);
    }
}