How to sort file names in ascending order?

前端 未结 9 2038
孤独总比滥情好
孤独总比滥情好 2020-12-05 10:30

I have a set of files in a folder, and all of them starting with a similar name, except one. Here is an example:

Coordinate.txt
Spectrum_1.txt
Spectrum_2.txt         


        
9条回答
  •  失恋的感觉
    2020-12-05 11:10

    Arrays.sort(fileList, new Comparator()
    {
        @Override
        public int compare(Object f1, Object f2) {
            String fileName1 = ((File) f1).getName();
            String fileName2 = ((File) f1).getName();
    
            int fileId1 = Integer.parseInt(fileName1.split("_")[1]);
            int fileId2 = Integer.parseInt(fileName2.split("_")[1]);
    
            return fileId1 - fileId2;
        }
    });
    

    make sure to handle files that does not has _ in the name

提交回复
热议问题