Skip to content

Custom sorting of faceted navigation


The category values in faceted navigation are sorted server side based on the facet's configuration which can be configured from the facet UI.

Custom sorting

If none of the built in ways of sorting are suitable, it is possible to implement your own custom sort order. To do this, a three step process is required:

  1. Write a groovy script or compile a custom comparator in java.
  2. Enable the custom comparator on the facet in the hook_post_process.groovy hook script.
  3. Choose when the custom comparator should apply in the faceted navigation user interface.

Implement the comparator

Comparators need to implement a java.util.Comparator. Here is an example of comparator which sorts on label.


import java.util.Comparator;


public class MyComparator implements Comparator<CategoryValue> {

    public int compare(CategoryValue o1, CategoryValue o2) {
        // lowercase the label as we don't want the sort to be case sensitive.
        return o1.getLabel().toLowerCase().compareTo(o2.getLabel().toLowerCase());

We could place our code in file:


The @groovy/com/foo path may need to be created.

Set the comparator on the facet

We will need to set the comparator on the facet in the hook_post_process.groovy hook script. To do this we will create or modify the file:


to contain:


for(Facet facet : transaction.getResponse().getFacets()) {
    // Find the facet we want to set our comparator on.
    if("Authors".equals(facet.getName())) {

Update the facet config to use the custom comparator

The facet configuration will need to be updated to include the CUSTOM_COMPARATOR sort order, this can be done with the facet editor UI. The custom sort may be used with other predefined sorting methods for example you can sort by count then by your custom comparator.


The data model returned by search endpoint will contain both response.facets[i].categories[n].values and response.facets[i].allValues. The former is a deprecated set of values that are returned to be compatible with previous versions of Funnelback which did not support this type of custom sorting. Only response.facets[i].allValues will have the custom sort applied and references to response.facets[i].categories[n].values should be updated to use the newer response.facets[i].allValues list instead.

See also


Funnelback logo