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:

The RuleBasedCollator 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 Stream.toList() method from Java 16 here — in earlier versions of Java you can just use .collect(Collectors.toList())):

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

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



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

Software developer @ JetBrains Space. I mostly write about Java and microservice architecture. Occasional rants on software development in general.