在java中按字母顺序排序文件"数字"

JRE*_*REN 5 java

我有一个文件夹,其中包含以时间戳命名的文件.

当我尝试浏览每个文件时,它按字母顺序排序并给我这个顺序:

/home/user/buffereddata/1
/home/user/buffereddata/100
/home/user/buffereddata/1000
/home/user/buffereddata/200
/home/user/buffereddata/2000
/home/user/buffereddata/300
Run Code Online (Sandbox Code Playgroud)

但我希望它们像这样排序:

/home/user/buffereddata/1
/home/user/buffereddata/100
/home/user/buffereddata/200
/home/user/buffereddata/300
/home/user/buffereddata/1000
/home/user/buffereddata/2000
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

File file = new File(System.getProperty("user.home") + "/buffereddata");

if(file.exists()) {
  File[] fileArray = file.listFiles();
  Arrays.sort(fileArray);
  for(File f : fileArray) {
    System.out.println(f);
  }
}
Run Code Online (Sandbox Code Playgroud)

是否有一些(最好是简单的)循环文件的方式,我想循环它们?

rle*_*ndi 11

Arrays.sort(fileArray, new Comparator<File>() {
    public int compare(File f1, File f2) {
        try {
            int i1 = Integer.parseInt(f1.getName());
            int i2 = Integer.parseInt(f2.getName());
            return i1 - i2;
        } catch(NumberFormatException e) {
            throw new AssertionError(e);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)


Luk*_*der 5

虽然其他答案在您的具体情况下是正确的(给定目录中的所有文件名都是数字),但这里有一个解决方案可以比较混合数字/非数字文件名,例如version-1.10.3.txt以直观的方式,类似于 Windows 的方式资源管理器这样做:

在此输入图像描述

这个想法(我已经在这里写了博客这个想法受到了这个答案的启发。)是将文件名分割成数字/非数字段,然后以数字方式比较两个文件名中的每个单独的段(如果两者都是)数字),或字母数字,否则:

public final class FilenameComparator implements Comparator<String> {
    private static final Pattern NUMBERS = 
        Pattern.compile("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
    @Override public final int compare(String o1, String o2) {
        // Optional "NULLS LAST" semantics:
        if (o1 == null || o2 == null)
            return o1 == null ? o2 == null ? 0 : -1 : 1;

        // Splitting both input strings by the above patterns
        String[] split1 = NUMBERS.split(o1);
        String[] split2 = NUMBERS.split(o2);
        for (int i = 0; i < Math.min(split1.length, split2.length); i++) {
            char c1 = split1[i].charAt(0);
            char c2 = split2[i].charAt(0);
            int cmp = 0;

            // If both segments start with a digit, sort them numerically using 
            // BigInteger to stay safe
            if (c1 >= '0' && c1 <= '9' && c2 >= 0 && c2 <= '9')
                cmp = new BigInteger(split1[i]).compareTo(new BigInteger(split2[i]));

            // If we haven't sorted numerically before, or if numeric sorting yielded 
            // equality (e.g 007 and 7) then sort lexicographically
            if (cmp == 0)
                cmp = split1[i].compareTo(split2[i]);

            // Abort once some prefix has unequal ordering
            if (cmp != 0)
                return cmp;
        }

        // If we reach this, then both strings have equally ordered prefixes, but 
        // maybe one string is longer than the other (i.e. has more segments)
        return split1.length - split2.length;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后您可以这样使用比较器:

Arrays.sort(fileArray, Comparators.comparing(File::getName, new FilenameComparator()));
Run Code Online (Sandbox Code Playgroud)