Fixed custom ordering of facets

We would like to fix the ordering of facets, for example we have facets for campus which we would like to always appear in a certain order.  Just ordered by value alphabetically at the minute.

 

Does anyone have any ideas / advice for achieving fixed custom ordering of facets?

 

Maybe a fixed order can be acheived using freemarker through the facets.ftl

Hi Gavin,

 

You can use the following code in the hook_post_process.groovy to sort your facets (this will sort them inside the object model):

transaction?.response?.facets.each() {
    // Rename 'YourFacet' as per your desired fact name
    if (it.name == "YourFacet") {
        it.categories.each() { c -> customSort(c) }
    }
}

// Recursively sorts FB facet categories using customCompare
def customSort(category) {
category.values.sort { a, b -> customCompare(a, b) }
category.categories.each() { f -> customSort(f) }
}

// Sorts a and b as per a static translation map
def customCompare(a, b) {
def labelOrderMap = [ “january” : “00001”,
“february” : “00002”,
“march” : “00003”,
“april” : “00004”,
“may” : “00005”,
“june” : “00006”,
“july” : “00007”,
“august” : “00008”,
“september” : “00009”,
“october” : “00010”];

  def label_a = a.label;
  def label_b = b.label;

  if (labelOrderMap.containsKey(label_a)) {
      label_a = labelOrderMap[label_a];
  }
  if (labelOrderMap.containsKey(label_b)) {
      label_b = labelOrderMap[label_b];
  }

  label_a.compareTo(label_b);

}

The example static map contains month names, so feel free to modify this as per your needs. Alternatively you could modify the signature of the customCompare function to be passed in the map (so you can pass in different maps for different facets).

 

Let me know if you have any further issues with this!

Thanks for the code.  I have added the code to the hook_post_process.groovy and ran an update.  Trouble it is not taking effect.

 

Any ideas what could be the issue?

transaction?.response?.facets.each() {
    if (it.name == "Level") {
        it.categories.each() { c -> customSort(c) }
    }
}

// Recursively sorts FB facet categories using customCompare
def customSort(category) {
category.values.sort { a, b -> customCompare(a, b) }
category.categories.each() { f -> customSort(f) }
}

// Sorts a and b as per a static translation map
def customCompare(a, b) {
def labelOrderMap = [ “Undergraduate” : “00001”,
“Postgraduate” : “00002”,
“Postgraduate Research” : “00003”,
“Short courses and CPD” : “00004”,
“eLearning” : “00005”];

  def label_a = a.label;
  def label_b = b.label;

  if (labelOrderMap.containsKey(label_a)) {
      label_a = labelOrderMap[label_a];
  }
  if (labelOrderMap.containsKey(label_b)) {
      label_b = labelOrderMap[label_b];
  }

  label_a.compareTo(label_b);

}

Hi Gavin, sorry it took so long, only just noticed this got another reply.

You can look at ~/web/logs/modernui.Public.log to see any errors. Barring that, do you have -rmc_sensitive=1 set in your query processor options? By default all values are lowercased, and only with the rmc_sensitive setting enabled do they revert to their original casing.
You could also lower-case the whole map, and explicitly lowercase the label_a and label_b in the containsKey check (that way you don’t care what the original casing was).