0

I want to process Android files alphabetically. I have tested that in Java on a PC, it sorted the files alphabetically. However, when I tested it in Android, I discovered that Android reads files by size.

Could you help me find a way using which I can read Android files alphabetically?

Android code:

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "testface" + File.separator;
Log.d("MyTag", path);

File folder = new File(path);
File[] listOfFiles = folder.listFiles();

Arrays.sort(listOfFiles);

Log.d("MyTag", "no.of files: " + listOfFiles.length);

long startTime2 = System.nanoTime();
for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        String filepath = path + listOfFiles[i].getName();
        Log.d("MyTag", "appface_image_filepath: " + filepath);
    }
}

Debugging log:

01-01 22:09:11.042 8484-8484/com.example.nasif.facedetectionloop D/MyTag: /storage/emulated/0/DCIM/testface/
01-01 22:09:11.043 8484-8484/com.example.nasif.facedetectionloop D/MyTag: no.of files: 10
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/11.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/12.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/17.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/18.jpg
01-01 22:09:11.044 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/19.jpg
01-01 22:09:11.045 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/20.jpg
01-01 22:09:11.045 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/27.jpg
01-01 22:09:11.045 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/28.jpg
01-01 22:09:11.046 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/3.jpg
01-01 22:09:11.046 8484-8484/com.example.nasif.facedetectionloop D/MyTag: appface_image_filepath: /storage/emulated/0/DCIM/testface/4.jpg
Omar Einea
  • 2,478
  • 7
  • 23
  • 35
user1850484
  • 388
  • 1
  • 3
  • 23
  • In fact, alphabetically, 28 is sorted before 3. That's all. For the answer, check [here](https://stackoverflow.com/a/16898539/5154891) – Thomas Mary Jan 01 '18 at 16:46

1 Answers1

1

You can implement your own Comparator.

Arrays.sort(listOfFiles, new Comparator<File>() {
    @Override
    public int compare(File f1, File f2) {
        return f1.getName().compareTo(f2.getName());
    }
});

If you want your files to be 1.jpg, 2.jpg... 10.jpg... then you can first compare the length of filename.

Arrays.sort(listOfFiles, new Comparator<File>() {
    @Override
    public int compare(File f1, File f2) {
        String name1 = f1.getName();
        String name2 = f2.getName();
        if (name1.length() == name2.length())
            return f1.getName().compareTo(f2.getName());
        return name1.length() - name2.length();
    }
});
Zoe
  • 27,060
  • 21
  • 118
  • 148
zhh
  • 2,346
  • 1
  • 11
  • 22