Helper for sorting arrays of objects based on multiple fields + orientations.
string $name:
int $orientation:
Closure $next:
Closure
public static function sortByField($name, $orientation = 1, \Closure $next = null) {
if (!$next) {
$next = function () {
return 0;
};
}
return function ($a, $b) use ($name, $next, $orientation) {
$aValue = ClosureExpressionVisitor::getObjectFieldValue($a, $name);
$bValue = ClosureExpressionVisitor::getObjectFieldValue($b, $name);
if ($aValue === $bValue) {
return $next($a, $b);
}
return ($aValue > $bValue ? 1 : -1) * $orientation;
};
}