If I have an object as such:
class Person {
var $age;
function __construct($age) {
$this->age = $age;
}
}
and I have any array
I just coded this. It should be faster than usort as it does not rely on numerous function calls.
function sortByProp($array, $propName, $reverse = false)
{
$sorted = [];
foreach ($array as $item)
{
$sorted[$item->$propName][] = $item;
}
if ($reverse) krsort($sorted); else ksort($sorted);
$result = [];
foreach ($sorted as $subArray) foreach ($subArray as $item)
{
$result[] = $item;
}
return $result;
}
Usage:
$sorted = sortByProp($people, 'age');
Oh, and it uses ksort but it works even if many $people are of the same $age.