if ( is_string( $a ) && is_string( $b ) ) {
return strcmp( $a, $b );
} elseif ( count( $a ) !== count( $b ) ) {
- return count( $a ) < count( $b ) ? -1 : 1;
+ return count( $a ) <=> count( $b );
} else {
reset( $a );
reset( $b );
public function compareStringLength( $s1, $s2 ) {
$l1 = strlen( $s1 );
$l2 = strlen( $s2 );
- if ( $l1 < $l2 ) {
- return 1;
- } elseif ( $l1 > $l2 ) {
- return -1;
- } else {
- return 0;
- }
+ return $l2 <=> $l1; // descending
}
/**
uksort( $logosPerDppx, function ( $a , $b ) {
$a = floatval( $a );
$b = floatval( $b );
-
- if ( $a == $b ) {
- return 0;
- }
// Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
- return ( $a < $b ) ? -1 : 1;
+ return $a <=> $b;
} );
foreach ( $logosPerDppx as $dppx => $src ) {
* @return int Result of string comparison, or namespace comparison
*/
public static function compare( LinkTarget $a, LinkTarget $b ) {
- if ( $a->getNamespace() == $b->getNamespace() ) {
- return strcmp( $a->getText(), $b->getText() );
- } else {
- return $a->getNamespace() - $b->getNamespace();
- }
+ return $a->getNamespace() <=> $b->getNamespace()
+ ?: strcmp( $a->getText(), $b->getText() );
}
/**
if ( $batchSize === 1 ) { // One user, can't be different
$ret = 0;
} elseif ( $this->orderBy === 'id' ) {
- $ret = $a[0]->rev_user - $b[0]->rev_user;
+ $ret = $a[0]->rev_user <=> $b[0]->rev_user;
} elseif ( $this->orderBy === 'name' ) {
$ret = strcmp( $a[0]->rev_user_text, $b[0]->rev_user_text );
} else {
- $ret = $a[0]->rev_actor - $b[0]->rev_actor;
+ $ret = $a[0]->rev_actor <=> $b[0]->rev_actor;
}
if ( !$ret ) {
}
if ( !$ret ) {
- $ret = $a[0]->rev_id - $b[0]->rev_id;
+ $ret = $a[0]->rev_id <=> $b[0]->rev_id;
}
return $neg * $ret;
$spec = [ 'sort2' => $i++ ] + $spec + [ 'sort' => 0 ];
}
unset( $spec );
+ // Sort according to the 'sort' field, and if they are equal, according to 'sort2'
usort( $specs, function ( $a, $b ) {
- return ( (int)$a['sort'] ) - ( (int)$b['sort'] )
- ?: $a['sort2'] - $b['sort2'];
+ return $a['sort'] <=> $b['sort']
+ ?: $a['sort2'] <=> $b['sort2'];
} );
$ret = [];
}
usort( $this->filters, function ( $a, $b ) {
- return $b->getPriority() - $a->getPriority();
+ return $b->getPriority() <=> $a->getPriority();
} );
foreach ( $this->filters as $filterName => $filter ) {
wfDebug( "Primary collision '$letter' '{$letterMap[$key]}' (comparison: $comp)\n" );
// If that also has a collision, use codepoint as a tiebreaker.
if ( $comp === 0 ) {
- // TODO Use <=> operator when PHP 7 is allowed.
- $comp = UtfNormal\Utils::utf8ToCodepoint( $letter ) -
+ $comp = UtfNormal\Utils::utf8ToCodepoint( $letter ) <=>
UtfNormal\Utils::utf8ToCodepoint( $letterMap[$key] );
}
if ( $comp < 0 ) {
*/
private function sortEntries() {
uasort( $this->entries, function ( $a, $b ) {
- return 10000 * ( $a['startTime'] - $b['startTime'] );
+ return $a['startTime'] <=> $b['startTime'];
} );
}
return function ( $a, $b ) use ( $key, $sub ) {
if ( isset( $a[$key] ) && isset( $b[$key] ) ) {
// Descending sort: larger values will be first in result.
- // Assumes all values are numeric.
// Values for 'main()' will not have sub keys
$valA = is_array( $a[$key] ) ? $a[$key][$sub] : $a[$key];
$valB = is_array( $b[$key] ) ? $b[$key][$sub] : $b[$key];
- return $valB - $valA;
+ return $valB <=> $valA;
} else {
// Sort datum with the key before those without
return isset( $a[$key] ) ? -1 : 1;
$cmpFunc = function ( $a, $b ) {
$al = substr_count( $a, '/' );
$bl = substr_count( $b, '/' );
- if ( $al === $bl ) {
- return 0; // should not actually happen
- }
- return ( $al < $bl ) ? 1 : -1; // largest prefix first
+ return $bl <=> $al; // largest prefix first
};
$matches = []; // matching prefixes (mount points)
* @return int Result of string comparison, or namespace comparison
*/
protected function compare( $a, $b ) {
- if ( $a->page_namespace == $b->page_namespace ) {
- return strcmp( $a->page_title, $b->page_title );
- } else {
- return $a->page_namespace - $b->page_namespace;
- }
+ return $a->page_namespace <=> $b->page_namespace
+ ?: strcmp( $a->page_title, $b->page_title );
}
/**
// Add on template profiling data in human/machine readable way
$dataByFunc = $this->mProfiler->getFunctionStats();
uasort( $dataByFunc, function ( $a, $b ) {
- return $a['real'] < $b['real']; // descending order
+ return $b['real'] <=> $a['real']; // descending order
} );
$profileReport = [];
foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) {
protected function getFunctionReport() {
$data = $this->getFunctionStats();
usort( $data, function ( $a, $b ) {
- if ( $a['real'] === $b['real'] ) {
- return 0;
- }
- return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
+ return $b['real'] <=> $a['real']; // descending
} );
$width = 140;
protected function getFunctionReport() {
$data = $this->getFunctionStats();
usort( $data, function ( $a, $b ) {
- if ( $a['real'] === $b['real'] ) {
- return 0;
- }
- return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
+ return $b['real'] <=> $a['real']; // descending
} );
$width = 140;
} );
// Sort descending by time elapsed
usort( $stats, function ( $a, $b ) {
- return $a['real'] < $b['real'];
+ return $b['real'] <=> $a['real'];
} );
array_walk( $stats,
* @return int Negative if $a < $b, positive if $a > $b, zero if equal
*/
public static function compare( $a, $b ) {
- return $a->getPriority() - $b->getPriority();
+ return $a->getPriority() <=> $b->getPriority();
}
}
$field['__index'] = $i++;
}
uasort( $formDescriptor, function ( $first, $second ) {
- return self::getField( $first, 'weight', 0 ) - self::getField( $second, 'weight', 0 )
- ?: $first['__index'] - $second['__index'];
+ return self::getField( $first, 'weight', 0 ) <=> self::getField( $second, 'weight', 0 )
+ ?: $first['__index'] <=> $second['__index'];
} );
foreach ( $formDescriptor as &$field ) {
unset( $field['__index'] );
];
usort( $this->filterGroups, function ( $a, $b ) {
- return $b->getPriority() - $a->getPriority();
+ return $b->getPriority() <=> $a->getPriority();
} );
foreach ( $this->filterGroups as $groupName => $group ) {
* @return int
*/
public function compare( $a, $b ) {
- if ( $a['name'] === $b['name'] ) {
- return 0;
- } else {
- return $this->getLanguage()->lc( $a['name'] ) > $this->getLanguage()->lc( $b['name'] )
- ? 1
- : -1;
- }
+ return $this->getLanguage()->lc( $a['name'] ) <=> $this->getLanguage()->lc( $b['name'] );
}
/**
// Sort results by version, then by filename, then by name.
foreach ( $this->foundNodes as $version => &$nodes ) {
uasort( $nodes, function ( $a, $b ) {
- return ( $a['filename'] . $a['name'] ) < ( $b['filename'] . $b['name'] ) ? -1 : 1;
+ return ( $a['filename'] . $a['name'] ) <=> ( $b['filename'] . $b['name'] );
} );
}
ksort( $this->foundNodes );
// break the tie by sorting by name
$origSpaces = $spaces;
uksort( $spaces, function ( $a, $b ) use ( $origSpaces ) {
- if ( $origSpaces[$a] < $origSpaces[$b] ) {
- return -1;
- } elseif ( $origSpaces[$a] > $origSpaces[$b] ) {
- return 1;
- } elseif ( $a < $b ) {
- return -1;
- } elseif ( $a > $b ) {
- return 1;
- } else {
- return 0;
- }
+ return $origSpaces[$a] <=> $origSpaces[$b]
+ ?: $a <=> $b;
} );
$ok = true;
global $sort;
switch ( $sort ) {
+ // Sorted ascending:
case 'name':
return strcmp( $a->name(), $b->name() );
+ // Sorted descending:
case 'time':
- return $a->time() > $b->time() ? -1 : 1;
+ return $b->time() <=> $a->time();
case 'memory':
- return $a->memory() > $b->memory() ? -1 : 1;
+ return $b->memory() <=> $a->memory();
case 'count':
- return $a->count() > $b->count() ? -1 : 1;
+ return $b->count() <=> $a->count();
case 'time_per_call':
- return $a->timePerCall() > $b->timePerCall() ? -1 : 1;
+ return $b->timePerCall() <=> $a->timePerCall();
case 'memory_per_call':
- return $a->memoryPerCall() > $b->memoryPerCall() ? -1 : 1;
+ return $b->memoryPerCall() <=> $a->memoryPerCall();
case 'calls_per_req':
- return $a->callsPerRequest() > $b->callsPerRequest() ? -1 : 1;
+ return $b->callsPerRequest() <=> $a->callsPerRequest();
case 'time_per_req':
- return $a->timePerRequest() > $b->timePerRequest() ? -1 : 1;
+ return $b->timePerRequest() <=> $a->timePerRequest();
case 'memory_per_req':
- return $a->memoryPerRequest() > $b->memoryPerRequest() ? -1 : 1;
+ return $b->memoryPerRequest() <=> $a->memoryPerRequest();
}
}
uasort(
$array,
function ( $a, $b ) {
- return serialize( $a ) > serialize( $b ) ? 1 : -1;
+ return serialize( $a ) <=> serialize( $b );
}
);
}