Last active
August 29, 2015 14:08
-
-
Save youssman/3c6a02c742dfcc354974 to your computer and use it in GitHub Desktop.
Sort array of objects by object fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
protected function compareFunction($a, $b){ | |
//any compare function (like strcmp, ...) or treatment we want | |
if ($a->objIntField == $b->objIntField) { | |
return 0; | |
} | |
return ( $a->objIntField > $b->objIntField ); | |
} | |
public function myAction(){ | |
// ... | |
// to preserve index use uasort | |
usort ( $myArray , array($this, 'compareFunction') ); | |
// ... | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment