You are here

public static function Semver::ksort in Markdown 8.2

Sorts an array where the keys are versions.

Note: this is basically just a strait copy of Semver's usort method. However, since they "final" all their classes, it cannot be extended from. Regardless, this assumes the version is the key.

Parameters

array $array: The array to sort, passed by reference.

int $direction: The sort direction.

See also

\Composer\Semver\Semver::usort()

2 calls to Semver::ksort()
Composer::getVersionHash in src/Util/Composer.php
Retrieves the pre-generated version hash.
MarkdownCommands::versionHash in src/Commands/MarkdownCommands.php
Run the markdown:version-hash hook.

File

src/Util/Semver.php, line 71

Class

Semver
Extends the base Composer SemVer class with additional functionality.

Namespace

Drupal\markdown\Util

Code

public static function ksort(array &$array, $direction = Semver::SORT_ASC) {
  $versionParser = static::versionParser();
  $normalized = [];
  foreach ($array as $key => $value) {
    $normalized[] = [
      $versionParser
        ->normalize($key),
      $key,
      $value,
    ];
  }
  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 sorted.
  $sorted = [];
  foreach ($normalized as $item) {
    list($key, $value) = array_slice($item, 1);
    $sorted[$key] = $value;
  }
  $array = $sorted;
}