You are here

class NumericSorter in Views XML Backend 8

Provides sorting for numbers.

Hierarchy

Expanded class hierarchy of NumericSorter

3 files declare their use of NumericSorter
Numeric.php in src/Plugin/views/sort/Numeric.php
Contains \Drupal\views_xml_backend\Plugin\views\sort\Numeric.
NumericSorterTest.php in tests/src/Unit/Sorter/NumericSorterTest.php
NumericTest.php in tests/src/Unit/Plugin/views/sort/NumericTest.php
Contains \Drupal\Tests\views_xml_backend\Unit\Plugin\views\sort\NumericTest.

File

src/Sorter/NumericSorter.php, line 15
Contains \Drupal\views_xml_backend\Sorter\NumericSorter.

Namespace

Drupal\views_xml_backend\Sorter
View source
class NumericSorter extends StringSorter {

  /**
   * {@inheritdoc}
   */
  public function __invoke(array &$result) {

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

}

Members

Namesort descending Modifiers Type Description Overrides
NumericSorter::__invoke public function Sorts a views result. Overrides StringSorter::__invoke
StringSorter::$direction protected property The direction to sort.
StringSorter::$field protected property The field of the result to sort.
StringSorter::__construct public function Constructs a StringSorter object.