I would like to sort an array of objects by a property of the specific object. This is my array with objects:
As you can see I have an array $all_studies
with 2 objects. How can I now sort on the graduationYear
property of the objects? So I would like to have an array with objects and the the order with object 2010 first, then 2014, ... (in this case the order is already correct but this won't always be the same ..).
This is what I've tried but with no succes:
$all_studies = usort($all_studies, "sort_objects_by_graduationyear");
function sort_objects_by_graduationyear($a, $b) {
if((int)$a->graduationYear == (int)$b->graduationYear){ return 0 ; }
return ($a->graduationYear < $b->graduationYear) ? -1 : 1;
}
But I just get true
back. I've never used the usort function so I don't really know how to work with it. Can someone help me?