You are here

public static function ClosureExpressionVisitor::sortByField in Plug 7

Helper for sorting arrays of objects based on multiple fields + orientations.

Parameters

string $name:

int $orientation:

\Closure $next:

Return value

\Closure

4 calls to ClosureExpressionVisitor::sortByField()
ArrayCollection::matching in lib/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php
Selects all elements from a selectable that match the expression and returns a new collection containing these elements.
ClosureExpressionVisitorTest::testSortByFieldAscending in lib/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php
ClosureExpressionVisitorTest::testSortByFieldDescending in lib/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php
ClosureExpressionVisitorTest::testSortDelegate in lib/doctrine/collections/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php

File

lib/doctrine/collections/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php, line 84

Class

ClosureExpressionVisitor
Walks an expression graph and turns it into a PHP closure.

Namespace

Doctrine\Common\Collections\Expr

Code

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;
  };
}