Sorting with RuleBasedCollator may stop working in Android 5.0

If you are using the RuleBasedCollator in your app, you may get NullpointerException for your collator object if you do not properly initialize its constructor with a correctly formatted rule string.

In Android 5.0 and later, the constructor expects to get a rule string that has a reset character in the first rule chain.

Refer to the documentation:

Note: earlier versions of RuleBasedCollator up to and including Android 4.4 (KitKat) allowed the omission of the reset from the first rule chain. This was interpreted as an implied reset after the last non-Han script in the default order. However, this is not a useful reset position, except for large tailorings of Han characters themselves. Starting with the CLDR 24 collation specification and the ICU 53 implementation, the initial reset is required.

So, if you had a rule string like this:

String rules = "<0<1<2<3<4<5<6<7<8<9"

And you initialize the collator like this:

RuleBasedCollator collator = new RuleBasedCollator(rules);

You would get a java ParseException:

java.text.ParseException: ucol_openRules failed: U_INVALID_FORMAT_ERROR (at offset -1)

The solution: Reset character

You have to add an initial reset character, which is: & to your rule string.

The new rule string then becomes:

String rules = "&<0<1<2<3<4<5<6<7<8<9"
comments powered by Disqus