0

I would like to get array with sorted filePaths. I have managed to get sorted array with filenames like this:

fileList.addAll(Arrays.asList(curFolder.list()));
Collections.sort(fileList);

and I know about such way of getting file paths:

folder.listFiles()

I also know that I can sort all file paths by name or date but I didn't manage to do it. In the end I will add this paths to my recyclerView adapter:

FileManagerAdapter fileManagerAdapter = new FileManagerAdapter(this, (ArrayList<String>) fileList, curFolder.listFiles());

maybe I have to get file path at adapter or what? I hope you will help me :)

Andrew
  • 1,947
  • 2
  • 23
  • 61

1 Answers1

0

I have solved my problem:

File[] files = curFolder.listFiles();

Arrays.sort(files, (f1, f2) -> {
if (f1.isDirectory() && !f2.isDirectory()) {
return -1;
} else if (!f1.isDirectory() && f2.isDirectory()) {
return 1;
} else {
return f1.compareTo(f2);
}
});

and as a result:

[/storage/emulated/0/Alarms, /storage/emulated/0/Android, /storage/emulated/0/DCIM, /storage/emulated/0/Download, /storage/emulated/0/Jobnetzwerk, /storage/emulated/0/Movies, /storage/emulated/0/Music, /storage/emulated/0/Notifications, /storage/emulated/0/Pictures, /storage/emulated/0/Podcasts, /storage/emulated/0/Ringtones]
Andrew
  • 1,947
  • 2
  • 23
  • 61