PHP Sort function for sorting an array of objects

Moonclan

New Member
I have an array full of objects from the same class. I'd like to sort this array by optional object field, for instance \[code\]$case->ID\[/code\] or \[code\]$case->Sender\[/code\]Is there a built in flavor of the array_sort() function that does this already, or will I have to write this sort function myself?Answer doesn't have to explain in detail - this is more like a yes/no questionThanksMy failed attempt at usort:\[code\]function sortBy($sort) { usort($this->abuseCases, function($a, $b) { if($a->{$sort} > $b->{$sort}) return 1; if($a->{$sort} < $b->{$sort}) return -1; else return 0; }); }\[/code\]Another failed attempt:\[code\] function sortBy($sort) { $this->sortBy = $sort; usort($this->abuseCases, array("this", "srt")); } private function srt($a, $b) { if($a->{$this->sortBy} > $b->{$this->sortBy}) return 1; if($a->{$this->sortBy} < $b->{$this->sortBy}) return -1; else return 0; }\[/code\]Edit for bump
 
Back
Top