W3cubDocs

/Drupal 8

public static function SortArray::sortByKeyInt

public static SortArray::sortByKeyInt($a, $b, $key)

Sorts an integer array item by an arbitrary key.

Parameters

array $a: First item for comparison.

array $b: Second item for comparison.

string $key: The key to use in the comparison.

Return value

int The comparison result for uasort().

File

core/lib/Drupal/Component/Utility/SortArray.php, line 121

Class

SortArray
Provides generic array sorting helper methods.

Namespace

Drupal\Component\Utility

Code

public static function sortByKeyInt($a, $b, $key) {
  $a_weight = (is_array($a) && isset($a[$key])) ? $a[$key] : 0;
  $b_weight = (is_array($b) && isset($b[$key])) ? $b[$key] : 0;

  if ($a_weight == $b_weight) {
    return 0;
  }

  return ($a_weight < $b_weight) ? -1 : 1;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!SortArray.php/function/SortArray::sortByKeyInt/8.1.x