Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Semver.php

function Semver::usort

Parameters

string[] $versions:

int $direction:

Return value

string[]

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) {
        $normalizedVersion = $versionParser->normalize($version);
        $normalizedVersion = $versionParser->normalizeDefaultBranch($normalizedVersion);
        $normalized[] = array(
            $normalizedVersion,
            $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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal