class NumericSorter in Views XML Backend 8
Provides sorting for numbers.
Hierarchy
- class \Drupal\views_xml_backend\Sorter\StringSorter implements SorterInterface
- class \Drupal\views_xml_backend\Sorter\NumericSorter
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\SorterView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NumericSorter:: |
public | function |
Sorts a views result. Overrides StringSorter:: |
|
StringSorter:: |
protected | property | The direction to sort. | |
StringSorter:: |
protected | property | The field of the result to sort. | |
StringSorter:: |
public | function | Constructs a StringSorter object. |