I'm trying to sort a list of files by date in custom format that appears in a file name itself. I'm not sure why, but this not works as expected.
This is how i get my files:
    ArrayList<File> inFiles = new ArrayList<>();
    File[] sortedByDate = parentDir.listFiles();
    for (File file : sortedByDate) {
        if(file.getName().endsWith(".txt")){
            inFiles.add(file);
        }
    }
}
Collections.sort(inFiles, new FileNameCompare());
The sort class:
public class FileNameCompare implements Comparator<File> {
    @Override
    public int compare(File o1, File o2) {
        String name1 = o1.getName();
        String name2 = o2.getName();
        String substr1 = name1.substring(name1.length()-24, name1.lastIndexOf('.'));
        String substr2 = name2.substring(name2.length()-24, name2.lastIndexOf('.'));
        //This is a file name format for example:
        //XXXXX_XXXX_24_Mar_2019_13_02_25.txt
        Date d1 = null, d2 = null;
        SimpleDateFormat sdf = new SimpleDateFormat( "dd_MMM_yyyy_hh_mm_ss" , Locale.ENGLISH);
        try {
            d1 = sdf.parse(substr1);
            d2 = sdf.parse(substr2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (int)(d1.getTime() - d2.getTime());
    }
}
For some reason this not working and return a non-sorted list. What I'm doing wrong?
Edited: Adding logs before and after the sorting.
Before:
25_Mar_2019_01_03_53.txt"
25_Mar_2019_01_21_44.txt"
25_Mar_2019_04_59_02.txt"
25_Mar_2019_05_57_06.txt"
25_Mar_2019_06_37_35.txt"
25_Mar_2019_07_10_07.txt"
18_Jan_2019_10_31_25.txt"
18_Jan_2019_10_25_25.txt"
24_Jan_2019_13_02_25.txt"
19_Feb_2019_13_02_25.txt"
18_Mar_2019_10_31_25.txt"
18_Mar_2019_10_25_25.txt"
24_Mar_2019_13_02_25.txt"
09_Jan_2019_09_36_25.txt"
09_Jan_2019_09_02_25.txt"
After:
18_Jan_2019_10_25_25.txt"
18_Jan_2019_10_31_25.txt"
24_Jan_2019_13_02_25.txt"
18_Mar_2019_10_25_25.txt"
18_Mar_2019_10_31_25.txt"
24_Mar_2019_13_02_25.txt"
25_Mar_2019_01_03_53.txt"
25_Mar_2019_01_21_44.txt"
25_Mar_2019_04_59_02.txt"
25_Mar_2019_05_57_06.txt"
25_Mar_2019_06_37_35.txt"
25_Mar_2019_07_10_07.txt"
19_Feb_2019_13_02_25.txt"
09_Jan_2019_09_02_25.txt"
09_Jan_2019_09_36_25.txt"
 
     
     
     
    