You are here

protected function DefaultFacetManager::sortFacetResults in Facets 8

Sort the facet results, and recurse to children to do the same.

Parameters

\Drupal\facets\Processor\SortProcessorInterface[] $active_sort_processors: An array of sort processors.

\Drupal\facets\Result\ResultInterface[] $results: An array of results.

Return value

\Drupal\facets\Result\ResultInterface[] A sorted array of results.

1 call to DefaultFacetManager::sortFacetResults()
DefaultFacetManager::build in src/FacetManager/DefaultFacetManager.php
Builds a facet and returns it as a renderable array.

File

src/FacetManager/DefaultFacetManager.php, line 452

Class

DefaultFacetManager
The facet manager.

Namespace

Drupal\facets\FacetManager

Code

protected function sortFacetResults(array $active_sort_processors, array $results) {
  uasort($results, function ($a, $b) use ($active_sort_processors) {
    $return = 0;
    foreach ($active_sort_processors as $sort_processor) {
      if ($return = $sort_processor
        ->sortResults($a, $b)) {
        if ($sort_processor
          ->getConfiguration()['sort'] == 'DESC') {
          $return *= -1;
        }
        break;
      }
    }
    return $return;
  });

  // Loop over the results and see if they have any children, if they do, fire
  // a request to this same method again with the children.
  foreach ($results as &$result) {
    if (!empty($result
      ->getChildren())) {
      $children = $this
        ->sortFacetResults($active_sort_processors, $result
        ->getChildren());
      $result
        ->setChildren($children);
    }
  }
  return $results;
}