Reputation:
How can I list the files of multiple directorys in Java?
That's what I've tried:
ArrayList<File> files = new ArrayList<File>();
for(int x = 0; x < directorys.length; x++) {
if(directorys[x].isDirectory()) {
files.addAll(Arrays.asList(directorys[x].listFiles(filter)));
}
}
That works kind of, but the problem is, that it doesn't bring the files into a fully alphabetic order.
It's than sth. like this: (first folder) 1, 3, 5, (second folder) 2, 4, 6. But I'd like it to be like this: 1, 2, 3, 4, 5, 6.
So I guess, what it does, is that it brings all the files of each folder into the correct order, but not all files together.
How could I achieve that?
Upvotes: 0
Views: 51
Reputation: 12937
You can just sort using a comparator:
files.sort((a,b)-> a.getName().compareTo(b.getName()));
Upvotes: 1
Reputation: 2013
You can sort the list of files after the loop like below
files.sort(Comparator.comparing(File::getName));
Upvotes: 1