I'm using drupal 7.28, and I have fetched user id using entity_load('user'), so I need descending sorting on usernames using this way of logic, let me know if anyone has an idea.Here is my code that I have written in my file.

$users = entity_load('user',$sort);
$usersIds = array_keys($users);
foreach($usersIds as $usrId)
   $user = user_load($usrId);
   $user_load_name = array($user->name);
   echo "<pre>";

1 Answer 1


small change in your code will do it for you

$users = entity_load('user');//load all user in your site
$usersIds = array_keys($users); // get all uid of the user
foreach($usersIds as $usrId) {
    $user = user_load($usrId); 
    $user_load_name = $user->name;//get the user name
    $user_load_name1[] = $user_load_name;// store all user name in array 
 natcasesort($user_load_name1);//For sorting array in ascending order INCASE SENCITIVE order
 $arr = array_reverse($user_load_name1, true);// For making it in descending  

This code will only give the user name in Decending order if you want it in ascending order just remove array_reverse() line that will be enough.

hope it helps you :)

  • Yes, working perfect :) Mar 3, 2015 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.