Troubleshoot sorting files by number size in Java

Source: Internet
Author: User

We want output (1.jpg, 2.jpg, 3.jpg, 10.jpg, 11.jpg, 20.jpg, 21.jpg, 31.jpg)

Suddenly see some online wording

Summary: Since you are dealing with a defined file name rule, you can also use regular expressions to simplify the use of

1file[] Files =file.listfiles ();2 if(files==NULL)returnResultfilename;3List<file> fileList =NewArraylist<file>();4  for(File f:files) {5 Filelist.add (f);6 }7 8Collections.sort (FileList,NewComparator<file>() {9 @OverrideTen      Public intCompare (file O1, file O2) { One         if(O1.isdirectory () &&o2.isfile ()) A             return-1; -         if(O1.isfile () &&o2.isdirectory ()) -             return1; the         returno2.getname (). CompareTo (O1.getname ()); -     } - }); -  +  for(File f:filelist) { -     if(F.isdirectory ()) { + Resultfilename.add (F.getpath ()); A ergodic (f,resultfilename); at}Else - Resultfilename.add (F.getpath ()); -}

But it outputs: (1.jpg, 10.jpg, 11.jpg, 2.jpg, 20.jpg, 21.jpg, 3.jpg, 31.jpg)

See this is clearly sorted in dictionary order since we want to sort by the integer size

1  Public Static voidMain (string[] args)throwsIOException {2File File =NewFile (path);3file[] Files =file.listfiles ();4arraylist<file> list =NewArraylist<>();5          for(File file2:files) {6 7 List.add (file2);8         }9         TenCollections.sort (list,NewComparator<file>() { One @Override A              Public intCompare (file O1, file O2) { -                 if(O1.isdirectory () &&o2.isfile ()) -                     return-1; the                 if(O1.isfile () &&o2.isdirectory ()) -                     return1; -Integer f =f (O1.getname ()); -Integer F2 =f (O2.getname ()); +                 returnInteger.compare (f, F2); -             } +         }); A          for(File file2:list) { at System.out.println (File2.getname ()); -         } -  -     } -      -     StaticInteger F (String filename) { in         intx = Filename.indexof (".")); -String string2 = filename.substring (0, x); to         Char[] cs =String2.tochararray (); +StringBuilder Builder =NewStringBuilder (); -          for(inti = 0; i < cs.length; i++) { the             if(Character.isdigit (Cs[i])) { * builder.append (Cs[i]); $             }Panax Notoginseng         } -         returnInteger.parseint (builder.tostring ()); the}

Using regular expressions

1  Public Final classFilestringImplementsComparable<filestring> {2     Private FinalString FileName;3     Private Final intPrefix_num;4Pattern number = Pattern.compile ("(\\d+) \ \. *");5 6      Publicfilestring (String fileName) {7          This. FileName =FileName;8Matcher Matcher =Number.matcher (fileName);9         if(Matcher.find ()) {TenPrefix_num = Integer.parseint (Matcher.group (1)); One}Else { APrefix_num = 0; -         } -     } the  - @Override -      Public intcompareTo (filestring o) { -         returnO.prefix_num > Prefix_num? -1:o.prefix_num = = Prefix_num? 0:1; +     } -  + @Override A      PublicString toString () { at         returnFileName; -     } -}

Troubleshoot sorting files by number size in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.