public static function ClosureExpressionVisitor::sortByField

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 drupal/core/vendor/doctrine/common/lib/Doctrine/Common/Collections/ArrayCollection.php
Select all elements from a selectable that match the criteria and return a new collection containing these elements.
ClosureExpressionVisitorTest::testSortByFieldAscending in drupal/core/vendor/doctrine/common/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php
ClosureExpressionVisitorTest::testSortByFieldDescending in drupal/core/vendor/doctrine/common/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php
ClosureExpressionVisitorTest::testSortDelegate in drupal/core/vendor/doctrine/common/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php

File

drupal/core/vendor/doctrine/common/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php, line 65

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