Created
May 9, 2011 15:00
-
-
Save Quinten/962674 to your computer and use it in GitHub Desktop.
sort an array of objects on the values of certain properties in mysql style
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
// sort an array of objects on the values of certain properties | |
// Usage: $obj_list = object_property_sort($obj_list, "title ASC"); | |
function object_property_sort($obj_arr, $order_by) { | |
$sort = explode(" ", $order_by); | |
$property = $sort[0]; | |
$dir = ($sort[1] == "ASC") ? SORT_ASC : SORT_DESC; | |
$sort_on_arr = array(); | |
foreach($obj_arr as $key => $object) | |
$to_sort_arr[(string)$key] = $object->$property; | |
$sorted_obj_arr = $obj_arr; | |
array_multisort($to_sort_arr, SORT_REGULAR, $dir, $sorted_obj_arr); | |
return $sorted_obj_arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hadden we zoiets al ni in php_multisort?