I'm trying to build my own sorting function implementing hook_facetapi_sort() and sorting the following data by weight.

name, weight
dog, -2
cat, 1
zebra, -5
dolphin, 3
bee, 5

The code I wrote so far is the following one.

function zoo_facetapi_sort_animal(array $a, array$b) {
  $a = db_query("select weight from {animal} where name = :a", array(':a' => $a['#markup']))->fetchAssoc();
  $b = db_query("select weight from {animal} where name = :b", array(':b' => $b['#markup']))->fetchAssoc();

  $a_value = (isset($a['weight'])) ? $a['weight'] : 0;
  $b_value = (isset($b['weight'])) ? $b['weight'] : 0;

  return strnatcmp($b_value, $a_value);

The result is what I expected.


I get the following error message.

Warning: uasort(): Array was modified by the user comparison function in FacetapiWidget->applySorts() (line 230 of /var/www/zoo.dev/sites/all/modules/facetapi/plugins/facetapi/widget.inc)

How do I avoid getting that message?

1 Answer 1


Register your own function as FacetAPI sort callback. Then select it in the FacetAPI sort order.



NB: You also accidentally collapsed the space between array and $b

function zoo_facetapi_sort_animal(array $a, array$b) {...}

 * Implements hook_facetapi_sort_info
function zoo_facetapi_sort_info() {
  $sorts = array();

  $sorts['active'] = array(
    'label' => t('Animal ...'), 
    'callback' => 'zoo_facetapi_sort_animal', 
    'description' => t('Sort by whether the animal is ...'), 
    'weight' => -50,

  return $sorts;

function zoo_facetapi_sort_animal(array $a, array $b) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.