Is there any Java open-source comparator for comparing beans by multiple fields for multi-column sorting? Each column can be sorted asceding or descending.
For single-column sorting it can be achieved by using org.apache.commons.beanutils.BeanComparator
together with org.springframework.util.comparator.InvertibleComparator
.
I'm aware that this functionality is quite trivial to write, but what's the benefit from reinventing the wheel, if it was already written and tested?
JSorter is another open source alternative for multi column sorting in Java. http://sourceforge.net/projects/jsorter/
I wrote this a few months ago.
Just inherit from this class and override the doCompare-Method. Then set a the next comparator in chain with
setNext()
. The earlier a comparator appears in this chain, the more "important" it is.EDIT:
Also see what I found: http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/comparators/ComparatorChain.html
This is part of the apache commons collection library, which you can download here
I recently wrote a Comparator to sort multiple fields within a delimited String record. It allows you to define the delimiter, record structure and sorting rules (some of which are type-specific).
Required information is seeded to the Comparator itself, either programmatically or through an XML file.
XML is validated by a package embedded XSD file. For example, below is a tab delimited record layout with four fields (two of which are sortable):
You would then use this in java like so:
Library can be found here:
http://sourceforge.net/projects/multicolumnrowcomparator/