Skip to content

Instantly share code, notes, and snippets.

@AntoineAugusti
Created August 10, 2013 13:02
Show Gist options
  • Save AntoineAugusti/6200354 to your computer and use it in GitHub Desktop.
Save AntoineAugusti/6200354 to your computer and use it in GitHub Desktop.
Sort array of users objects, based on their name
<?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