使わないかもしれないけど。
public final class FilePathComparator implements Comparator<File>{ private String splitCharacter = File.separator.equals("\\") ? "\\\\" : "/"; private OrderBy orderby; public enum OrderBy{ ASC(1), DESC(-1); private int v; private OrderBy(int v){ this.v = v; } public int getVector(){ return v; } } public FilePathComparator(OrderBy orderby){ this.orderby =orderby; } @Override public int compare(File o1, File o2){ return new Integer(o1.getPath().split(splitCharacter).length) .compareTo(new Integer(o2.getPath().split(splitCharacter).length)) * orderby.getVector(); } }
この中の enum OrderBy で、昇順、降順を指定する。
使用例、
List<File> list; // 省略... list.stream().sorted(new FilePathComparator(OrderBy.ASC)).forEach(e->{ });