allowing sort by username and first_name attribute in userList

Permalink
Hi folks,

On my site I have registration by username and also some first name and last name attributes.

On the members page, how can I sort the user list by both username and (if declared) first_name so that they all show up in ascending order?

I have tried the following in the members.php controller but it throws a mysql error...

$userList = new UserList();
$users = $userList->get();
foreach ($users as $user) {
if ($user->getAttribute('first_name') == ''){
$sortName = $user->getUserName();
} else {
$sortName = $user->getAttribute('first_name').' '.$user->getAttribute('last_name');
}
}
$userList->sortBy($sortName, 'asc');

Any ideas on how to do this?