You are here

public static function RevisionTreeIndex::sortRevisions in Multiversion 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Index/RevisionTreeIndex.php \Drupal\multiversion\Entity\Index\RevisionTreeIndex::sortRevisions()

Parameters

array $a:

array $b:

Return value

integer

Overrides RevisionTreeIndexInterface::sortRevisions

File

src/Entity/Index/RevisionTreeIndex.php, line 217

Class

RevisionTreeIndex
The revision tree index.

Namespace

Drupal\multiversion\Entity\Index

Code

public static function sortRevisions(array $a, array $b) {
  $a_deleted = $a['#rev_info']['status'] == 'deleted' ? TRUE : FALSE;
  $b_deleted = $b['#rev_info']['status'] == 'deleted' ? TRUE : FALSE;

  // The goal is to sort winning revision candidates from low to high. The
  // criteria are:
  // 1. Non-deleted always win over deleted.
  // 2. When IDs match, higher ASCII sort on revision hash wins.
  // 3. Otherwise, the highest ID wins.
  if ($a_deleted && !$b_deleted) {
    return 1;
  }
  elseif (!$a_deleted && $b_deleted) {
    return -1;
  }
  list($a_id) = explode('-', $a['#rev']);
  list($b_id) = explode('-', $b['#rev']);
  if ($a_id === $b_id) {
    return $a['#rev'] < $b['#rev'] ? 1 : -1;
  }
  else {
    return $a_id < $b_id ? 1 : -1;
  }
}