fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6.  
  7.  
  8. class FilesByExtension
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String [] arrFiles =
  13. {"b.txt", "a.txt", "a.doc", "b.doc", "z.aaa",
  14. ".htaccess", "a.zzz", "noextension", ".nonombre",
  15. "a1.txt", "a11.txt", "11z.html", "^fake.exe",
  16. "numextension.11txt", "espacioextension. <-un espacio!", "extensionsimbolo@email.@com",
  17. "split.py"
  18. };
  19. String [] arrFilesOk = dirListByExtension(arrFiles);
  20. System.out.println(Arrays.toString(arrFilesOk));
  21. }
  22.  
  23. public static String [] dirListByExtension(String [] arrFiles) {
  24. Arrays.sort(arrFiles, new Comparator<String>() {
  25. @Override
  26.  
  27. public int compare(String ss1, String ss2) {
  28. String s1 = ss1.toLowerCase();
  29. String s2 = ss2.toLowerCase();
  30. final int s1Dot = s1.lastIndexOf('.');
  31. final int s2Dot = s2.lastIndexOf('.');
  32. //
  33. if ((s1Dot == -1) == (s2Dot == -1)) { // both or neither
  34. s1 = s1.substring(s1Dot + 1);
  35. s2 = s2.substring(s2Dot + 1);
  36. return s1.compareTo(s2);
  37. } else if (s1Dot == -1) { // only s2 has an extension, so s1 goes first
  38. return -1;
  39. } else { // only s1 has an extension, so s1 goes second
  40. return 1;
  41. }
  42. }
  43. });
  44. return arrFiles;
  45. /*
  46.   Arrays.sort(arrFiles, new Comparator<String>() {
  47.  
  48.   @Override
  49.   public int compare(String s1, String s2) {
  50.   final int s1Dot = s1.lastIndexOf('.');
  51.   final int s2Dot = s2.lastIndexOf('.');
  52.   if ((s1Dot == -1) == (s2Dot == -1)) {
  53.   s1 = s1.substring(s1Dot + 1);
  54.   s2 = s2.substring(s2Dot + 1);
  55.   return s1.compareTo(s2);
  56.   } else if (s1Dot == -1) {
  57.   return -1;
  58.   } else {
  59.   return 1;
  60.   }
  61.   }
  62. });
  63.   return arrFiles;
  64.  
  65.  
  66. */
  67.  
  68. }
  69. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
[noextension, espacioextension. <-un espacio!, numextension.11txt, extensionsimbolo@email.@com, z.aaa, a.doc, b.doc, ^fake.exe, .htaccess, 11z.html, .nonombre, split.py, b.txt, a.txt, a1.txt, a11.txt, a.zzz]