Skip to content

Instantly share code, notes, and snippets.

@gschoppe
Created May 20, 2015 02:36
Show Gist options
  • Save gschoppe/3cf8560d1b79452ce068 to your computer and use it in GitHub Desktop.
Save gschoppe/3cf8560d1b79452ce068 to your computer and use it in GitHub Desktop.
Surname Sort
<?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