usort and uasort use a comparison function which is slow because it must be computed every time a comparison is needed between to elements of an array. Other languages, like Python, let you sort an array using a key function which gets evaluated only once per element in the array. What’s the best way to do this in PHP?
Share
function sort_with_keyfunc($array, $keyfunc) { $keys = array_map($keyfunc, $array); // get the keys for each item array_multisort($keys, $array); // sort $array according to the sorted keys return $array; }This also maintains key=>value pairs in associative arrays.