Skip to content

Instantly share code, notes, and snippets.

@Quinten
Created May 9, 2011 15:00
Show Gist options
  • Select an option

  • Save Quinten/962674 to your computer and use it in GitHub Desktop.

Select an option

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
// 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;
}
@TimBroddin

Copy link
Copy Markdown

hadden we zoiets al ni in php_multisort?

@Quinten

Quinten commented May 10, 2011

Copy link
Copy Markdown
Author

Zou kunnen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment