Q:

How can I remove leading "the"s during a Java sort?

I have an Android ListView I would like to sort. Currently I am using Collections.sort which alphabetizes just fine.
However, I would like to take one step further and move the leading "the".  Is there a built in Java or Android method that can handle this?
If not, what is the code I would need?

A:

Use Collections.sort() with a custom Comparator<String> that strips out leading "The"s before comparing strings.
List<String> foo = /* init */;

Comparator<String> ignoreLeadingThe = new Comparator<String>() {
    @Override public int compare(String a, String b) {
        // TODO check for nulls?

        // (?i) makes the match case-insensitive
        a = a.replaceAll("(?i)^the\\s+", "");
        b = b.replaceAll("(?i)^the\\s+", "");

        // TODO use compareToIgnoreCase() instead?
        return a.compareTo(b);
    }
};

Collections.sort(foo, ignoreLeadingThe);

Demo: http://ideone.com/6hogi

