Skip to content

Instantly share code, notes, and snippets.

@joemaller
Created January 22, 2013 03:06
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 joemaller/4591740 to your computer and use it in GitHub Desktop.
Save joemaller/4591740 to your computer and use it in GitHub Desktop.
This works, but not well enough. Needs better handling of undefined values and arranging things in fixed lengths. For example, if there three items and only one has a sort key, but it's sort key asks to be in the second place, then the item specifying spot two should get spot two and all the undefined items should maintain order around it. Funda…
public function array_sortByKey($arr, $key)
{
$sort_on_key = function ($a, $b) use ($key) {
$a[$key] = (isset($a[$key])) ?: null;
$b[$key] = (isset($b[$key])) ?: null;
return strnatcmp($a[$key], $b[$key]);
};
usort($arr, $sort_on_key);
return $arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment