Ignore the word "the" when sorting a Java collection

I currently have code that accepts file names and sorts them differently (region-sensitive natural sort, case-insensitive ASCII-bit type, date modified, etc.), for example, for locale-sensitive natural The varieties he uses are:

Collections.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { return NaturalComparator.compareNatural(collator, f1.getName(), f2.getName()); } }); 

I would like to know how to make it ignore the word "the" at the beginning of the file name, so instead of ordering such files:

Apple
Carrot
Banana

He orders them as follows:

Apple
Banana

Carrot

+4
source share
4 answers

You can replace the first occurrence of The before passing it to the comparator using String#replaceFirst(regex, replacement) or String.replaceAll(regex, replacement) , both of which take the regex parameter to replace, and both can be used here: -

 @Override public int compare(File f1, File f2) { return NaturalComparator.compareNatural(collator, f1.getName().replaceAll("^(?i)The ", ""), f2.getName().replaceAll("^(?i)The ", "")); } 

Added a flag (?i) before regex pattern to replace case insensitive (thanks @Chris for pointing this out).

Caret (^) added before the pattern, so that it only replaces The at the beginning of the line. So, The banana will be replaced by banana , but Banana, The will not be replaced by Banana, He will remain the same.

+5
source
 Collections.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { return NaturalComparator.compareNatural(collator, f1.getName().startsWith("The ")?f1.getName().substring(4) : f1.getName(), f2.getName().startsWith("The ")?f2.getName().substring(4):f2.getName(); } }); 
0
source
 static String ignoreThe(String s) { if (s.length > 3) { if (s.substring(0, 4).toLowerCase().equal("the ")) { return s.substring(0, 4); } } return s; } Collections.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { String name1 = ignoreThe(f1.getName()); String name1 = ignoreThe(f2.getName()); return name1.compareTo(name2); } }); 
0
source

try

 ... return NaturalComparator.compareNatural(collator, f1.getName().replaceAll("^The ", ""), f2.getName().replaceAll("^The ", "")); 
0
source

All Articles