fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. List<String> foo = new ArrayList<String>();
  9. foo.add("one");
  10. foo.add("two");
  11. foo.add("The abc");
  12. foo.add("THE zzz");
  13. foo.add("one the");
  14.  
  15. System.out.println(foo);
  16.  
  17. Comparator<String> ignoreLeadingThe = new Comparator<String>() {
  18. @Override public int compare(String a, String b) {
  19. // TODO check for nulls?
  20.  
  21. // (?i) makes the match case-insensitive
  22. a = a.replaceAll("(?i)^the\\s+", "");
  23. b = b.replaceAll("(?i)^the\\s+", "");
  24.  
  25. // TODO use compareToIgnoreCase() instead?
  26. return a.compareTo(b);
  27. }
  28. };
  29.  
  30. Collections.sort(foo, ignoreLeadingThe);
  31.  
  32. System.out.println(foo);
  33. }
  34. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
[one, two, The abc, THE zzz, one the]
[The abc, one, one the, two, THE zzz]