Custom String Comparison in Java

Image by analogicus from Pixabay

A hidden gem in the standard Java library is the RuleBasedCollator class. It allows you to define custom collation — a set of rules for comparison between different characters in a string — in a flexible way. Your use cases might be one of the following:

  • you need to sort strings with uppercase characters with higher or lower priority than lowercase characters;
  • you want to set higher or lower precedence for characters with an accent (like à) or any other non-Latin characters;
  • you need to specify any other non-standard rules for character precedence that would be too complicated to define in a implementation.

The class allows you to set the rules in a convenient and flexible manner, just by writing them as a string expression. For instance, if you sort the following list in a usual order, you get the expected result (I’m using the method from Java 16 here — in earlier versions of Java you can just use ):

If you now want letter to rank higher than letter but keep all the other comparison rules the same, you could write such rule as follows: . Here it is in practice:

There are more complicated examples of usage of the class in the JavaDocs— check them out to know more.

Technical project lead @ CRX Markets. I mostly write about Java and microservice architecture. Occasional rants on software development in general.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store