You are here

public function StringSorter::__invoke in Views XML Backend 8

Sorts a views result.

Parameters

\Drupal\views\ResultRow[] &$result: The views result.

Overrides SorterInterface::__invoke

2 methods override StringSorter::__invoke()
DateSorter::__invoke in src/Sorter/DateSorter.php
Sorts a views result.
NumericSorter::__invoke in src/Sorter/NumericSorter.php
Sorts a views result.

File

src/Sorter/StringSorter.php, line 47
Contains \Drupal\views_xml_backend\Sorter\StringSorter.

Class

StringSorter
Provides sorting for strings.

Namespace

Drupal\views_xml_backend\Sorter

Code

public function __invoke(array &$result) {

  // Notice the order of the arguments to strcasecmp().
  switch ($this->direction) {
    case 'ASC':
      usort($result, function (ResultRow $a, ResultRow $b) {
        $compare = strcasecmp(reset($a->{$this->field}), reset($b->{$this->field}));
        if ($compare === 0) {
          return $a->index < $b->index ? -1 : 1;
        }
        return $compare;
      });
      break;
    case 'DESC':
      usort($result, function (ResultRow $a, ResultRow $b) {
        $compare = strcasecmp(reset($b->{$this->field}), reset($a->{$this->field}));
        if ($compare === 0) {
          return $a->index < $b->index ? -1 : 1;
        }
        return $compare;
      });
      break;
  }
}