Besides building your own Collector to customize how the results are stored and sorted, another way to customize results sorting is to implement your own FieldComparator
for SortField
. Note that this customization only deals with the sorting aspect of the result set; it does not give you the flexibility to filter out results, as you would have with Collector.
To implement a FieldComparator
, we need to implement two classes: FieldComparator
and FieldComparatorSource
. The FieldComparator
is the underlying class that contains the methods for comparing the documents in the result set. The FieldComparatorSource
is a class that instantiates FieldComparator
. It acts like a wrapper on FieldComparator
. To use it, we pass our own FieldComparatorSource
in the place of SortField.Type
into the constructor of Sor
tField
.