I donβt think you will get many ready-made comparators, but Guava has Ordering , which extends the functionality of Comparator and adds some useful default implementations as a factory method
And also: Guava and Apache Commons / Lang (there: I said that) will help you implement custom comparators or comparative examples using CompareToBuilder and ComparisonChain , respectively. I'm afraid this is not much better.
And about these requirements:
There are so many other reusable useful features that arent available.
- ignoring spaces
- gap normalization
- lines with numbers - for example, "apple 10"> "apple 2".
It is not recommended to do this in the comparator, because this means that your unchanged data remains in the collection, and Comparator needs to do the required conversion twice for each comparison. Now think about sorting an array with several million records. How many string conversions will it take?
It is always wiser to normalize your data first and then sort them.
Sean Patrick Floyd
source share