Title: Sorting Array with User Id
Last modified: August 22, 2016

---

# Sorting Array with User Id

 *  [ferguson4848](https://wordpress.org/support/users/ferguson4848/)
 * (@ferguson4848)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/sorting-array-with-user-id/)
 * I’ve got a function that that retrieves all the user names along with the number
   of published posts. Then, I put both of those variables into an array. This works
   great, but I would like to sort the array by the number of posts. So in this 
   case sort it by the Value.
 * I know there is a asort(), but it does not to seem have effect on the order of
   the array.
 * Not sure what I’m doing wrong.
 * Thanks, the code is below.
 *  $data = array($nickname => $num_rows);
    arsort($data); foreach ($data as $key
   => $value) { echo $key . ‘ : ‘ . $value. ‘‘; }

Viewing 1 replies (of 1 total)

 *  [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * (@lorro)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/sorting-array-with-user-id/#post-5953247)
 * I’m a usort fan:
 *     ```
       function compareNumber($data_1, $data_2) {
         if ($data_1->num_rows == $data_2->num_rows) return 0;
         return ($data_1->num_rows < $data_2->num_rows) ? -1 : 1;
       }
       usort($data, 'compareNumber');
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Sorting Array with User Id’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [sort](https://wordpress.org/support/topic-tag/sort/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/sorting-array-with-user-id/#post-5953247)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
