How to sort an array of string data using RuleBasedCollator class?
We can use the
java.text.Collator class to sort strings in language-specific order. Using the
java.text.Collator class makes the string not just sorted by the ASCII code of their characters but it will follow the language natural order of the characters.
If the predefined collation rules do not meet your needs, you can design your own rules and assign them to a
RuleBasedCollator object. Customized collation rules are contained in a
String object that is passed to the
Below is the result of sorting strings using a different
[apple, banana, carrot, apricot, blueberry, cabbage]
rule: < a < b < c
[apple, apricot, banana, blueberry, cabbage, carrot]
rule: < c < b < a
[cabbage, carrot, banana, blueberry, apple, apricot]
rule: < c < a < b
[cabbage, carrot, apple, apricot, banana, blueberry]