You are here

public function OgResolvedGroupCollection::sort in Organic groups 8

Sorts the groups in the collection according to their vote count.

The weight of each vote is taken into account.

Overrides OgResolvedGroupCollectionInterface::sort

File

src/OgResolvedGroupCollection.php, line 92

Class

OgResolvedGroupCollection
Contains a collection of groups discovered by OgGroupResolver plugins.

Namespace

Drupal\og

Code

public function sort() {

  // Find the best matching group by iterating over the candidates and return
  // the one that has the most "votes". If there are multiple candidates with
  // the same number of votes then the candidate that was resolved by the
  // plugin(s) with the highest priority will be returned.
  uasort($this->groupInfo, function ($a, $b) {
    if (count($a['votes']) == count($b['votes'])) {
      return array_sum($a['votes']) < array_sum($b['votes']) ? 1 : -1;
    }
    return count($a['votes']) < count($b['votes']) ? 1 : -1;
  });
}