Created
January 22, 2013 03:06
-
-
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…
This file contains 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
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