Skip to content

Instantly share code, notes, and snippets.

@youssman
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youssman/3c6a02c742dfcc354974 to your computer and use it in GitHub Desktop.
Save youssman/3c6a02c742dfcc354974 to your computer and use it in GitHub Desktop.
Sort array of objects by object fields
<?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