Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utils.php
Show First 20 Lines • Show All 388 Lines • ▼ Show 20 Lines | |||||
* @param list List of objects to sort by some property. | * @param list List of objects to sort by some property. | ||||
* @param string Name of a method to call on each object; the return values | * @param string Name of a method to call on each object; the return values | ||||
* will be used to sort the list. | * will be used to sort the list. | ||||
* @return list Objects ordered by the return values of the method calls. | * @return list Objects ordered by the return values of the method calls. | ||||
*/ | */ | ||||
function msort(array $list, $method) { | function msort(array $list, $method) { | ||||
$surrogate = mpull($list, $method); | $surrogate = mpull($list, $method); | ||||
// See T13303. A "PhutilSortVector" is technically a sortable object, so | |||||
// a method which returns a "PhutilSortVector" is suitable for use with | |||||
// "msort()". However, it's almost certain that the caller intended to use | |||||
// "msortv()", not "msort()", and forgot to add a "v". Treat this as an error. | |||||
if ($surrogate) { | |||||
$item = head($surrogate); | |||||
if ($item instanceof PhutilSortVector) { | |||||
throw new Exception( | |||||
pht( | |||||
'msort() was passed a method ("%s") which returns '. | |||||
'"PhutilSortVector" objects. Use "msortv()", not "msort()", to '. | |||||
'sort a list which produces vectors.', | |||||
$method)); | |||||
} | |||||
} | |||||
asort($surrogate); | asort($surrogate); | ||||
$result = array(); | $result = array(); | ||||
foreach ($surrogate as $key => $value) { | foreach ($surrogate as $key => $value) { | ||||
$result[$key] = $list[$key]; | $result[$key] = $list[$key]; | ||||
} | } | ||||
return $result; | return $result; | ||||
▲ Show 20 Lines • Show All 1,424 Lines • Show Last 20 Lines |