Created
August 10, 2013 13:02
-
-
Save AntoineAugusti/6200354 to your computer and use it in GitHub Desktop.
Sort array of users objects, based on their name
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 | |
class User | |
{ | |
protected $name; | |
/* | |
Returns < 0 if $a->name is less than $b->name; > 0 if $a->name is greater than $b->name, and 0 if they are equal. | |
*/ | |
static function sortUsersName(User $a, User $b) | |
{ | |
return strcmp($a->name, $b->name); | |
} | |
static function sortUsers($users) | |
{ | |
if (is_array($users) | |
{ | |
// sortUsersName MUST be a static class method in this case | |
usort($users, array('User', 'sortUsersName')); | |
return $users | |
} | |
else | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment