Created
May 20, 2015 02:36
-
-
Save gschoppe/3cf8560d1b79452ce068 to your computer and use it in GitHub Desktop.
Surname Sort
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
<?php | |
usort($array, function($a, $b) { | |
// explode name a by spaces | |
$parts_a = explode(' ', $a); | |
// remove the last name from the end of the array | |
$last_a = array_pop($parts_a); | |
// and shove it on the front | |
array_unshift($parts_a, $last_a); | |
// then re-implode into a string | |
$a = implode(' ', $parts_a); | |
// repeat the process for name b | |
$parts_b = explode(' ', $b); | |
$last_b = array_pop($parts_b); | |
array_unshift($parts_b, $last_b); | |
$b = implode(' ', $parts_b); | |
// perform a case insensitive string comparison | |
return strcasecmp($a, $b); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment