I looked up sorting files in directory by size using java array list && How to sort an ArrayList by its elements size in Java?
My question is what is the best way for the Comparator to be implemented so that sort will be faster? I was told that the sort for 100k files should be done in seconds not in minutes, as the file sizes are in long. Is there a better way to implement the Comparator?
My Comparator is:
public static List<File> sortFilesBySize(List<File> xmlFileList) {
xmlFileList.sort(Comparator.comparing(File::length).reversed());
return xmlFileList;
}
where
private static List<File> xmlFileList = new ArrayList<File>();
xmlFileList is populated as:
pathList = pathList.subList(0,filterCount);
for (Path filePath : pathList)
xmlFileList.add(filePath.toFile());
filterCount is how I filter by number of files to be sorted
and the sortFilesBySize is invoked as:
long startSortMillis = System.currentTimeMillis();
sortFilesBySize(xmlFileList);
long timeInMillis = System.currentTimeMillis() - startSortMillis;
By varying the number of files sorted as 5k, 10k 20k etc. I get
- 5k ----> 1329 ms
- 10k ---> 2808 ms
- 20k ---> 29790 ms
- 40k ---> 428408 ms
- 80k ---> 838658 ms
- 100k --> 1159034 ms
It can be observed that after 20k the sort takes minutes. Any suggestions how I can lower the sort time?
I also looked up https://docs.oracle.com/javase/8/docs/api/java/io/File.html to see if I could improve my current implementation, but nothing seemed to jump out.