You are here

private static function Semver::usort in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/composer/semver/src/Semver.php \Composer\Semver\Semver::usort()

Parameters

array $versions:

int $direction:

Return value

array

2 calls to Semver::usort()
Semver::rsort in vendor/composer/semver/src/Semver.php
Sort given array of versions in reverse.
Semver::sort in vendor/composer/semver/src/Semver.php
Sort given array of versions.

File

vendor/composer/semver/src/Semver.php, line 92

Class

Semver

Namespace

Composer\Semver

Code

private static function usort(array $versions, $direction) {
  if (null === self::$versionParser) {
    self::$versionParser = new VersionParser();
  }
  $versionParser = self::$versionParser;
  $normalized = array();

  // Normalize outside of usort() scope for minor performance increase.
  // Creates an array of arrays: [[normalized, key], ...]
  foreach ($versions as $key => $version) {
    $normalized[] = array(
      $versionParser
        ->normalize($version),
      $key,
    );
  }
  usort($normalized, function (array $left, array $right) use ($direction) {
    if ($left[0] === $right[0]) {
      return 0;
    }
    if (Comparator::lessThan($left[0], $right[0])) {
      return -$direction;
    }
    return $direction;
  });

  // Recreate input array, using the original indexes which are now in sorted order.
  $sorted = array();
  foreach ($normalized as $item) {
    $sorted[] = $versions[$item[1]];
  }
  return $sorted;
}