How can I sort this array of objects by one of its fields, like name or count ?

  Array
(
    [0] => stdClass Object
        (
            [ID] => 1
            [name] => Mary Jane
            [count] => 420
        )

    [1] => stdClass Object
        (
            [ID] => 2
            [name] => Johnny
            [count] => 234
        )

    [2] => stdClass Object
        (
            [ID] => 3
            [name] => Kathy
            [count] => 4354
        )

   ....
share|improve this question

4 Answers

up vote 41 down vote accepted

Use usort, here's an example adapted from the manual:

function cmp($a, $b)
{
    return strcmp($a->name, $b->name);
}

usort($your_data, "cmp");
share|improve this answer

Heres a nicer way using closures

usort($your_data, function($a, $b)
{
    return strcmp($a->name, $b->name);
});

Please note this is not in PHP's documentation but if you using 5.3+ closures are supported where callable arguments can be provided.

share|improve this answer
1  
I love this one better than the accepted answer since we can quickly define the compare function and can use in a class – Nam G. VU May 1 '12 at 18:43
This worked for me, however the accepted answer did not. The class would not recognize the callback function by name. I am not sure why. Thanks – I am John Galt Sep 5 '12 at 15:25
usort($array, 'my_sort_function');

var_dump($array);

function my_sort_function($a, $b)
{
    return $a->name < $b->name;
}

The same code will be with the count field.

More details about usort: http://ru2.php.net/usort

Btw, where did you get that array from? I hope that not from database?

share|improve this answer
Actually $result will contain TRUE if it's successful, and your comparison should be $a->name > $b->name. :) – cambraca Nov 26 '10 at 3:56
1  
@cambraca: oh, forgot it accepts array by reference. Btw, OP did not said which order he need to sort collection. – zerkms Nov 26 '10 at 3:57
well yes, it's a database :) actually from a function that gets the data from the database – Alex Nov 26 '10 at 4:01
1  
@Alex: why don't you sort it in database then? ORDER BY count – zerkms Nov 26 '10 at 4:02
it's more complicated, because that's a stadard function part of wordpress, and as I'm writing a plugin, I can't change wp files. I tried your example using create_function (because I'm using it inside a class and I don't know how to pass the function name to usort): create_function('$a,$b', "return $a->count < $b->count;") but I can't make it work :( I get a few notices and warning that usort expects parameter 2 to be a valid callback – Alex Nov 26 '10 at 4:11
show 2 more comments

if you're using php oop you might need to change to:

public static function cmp($a, $b) 
{
    return strcmp($a->name, $b->name);
}

usort($your_data, array('YOUR_CLASS_NAME','FUNCTION_NAME'); //in this case function_name = cmp
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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