NumericSorter.php in Views XML Backend 8
File
src/Sorter/NumericSorter.php
View source
<?php
namespace Drupal\views_xml_backend\Sorter;
use Drupal\views\ResultRow;
class NumericSorter extends StringSorter {
public function __invoke(array &$result) {
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;
}
}
}