Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
add comment (requires an account with 50 reputation)

5 Answers

up vote 61 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
add comment (requires an account with 50 reputation)

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
@IamJohnGalt - Maybe this is relevant: look this comment on php.net – Bruno May 28 at 22:39
add comment (requires an account with 50 reputation)
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 commentsadd comment (requires an account with 50 reputation)

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
Excelent !!!!!! – ramiromd Jun 5 at 16:05
add comment (requires an account with 50 reputation)

If you need local based string comparison, you can use strcoll instead of strcmp.

Remeber to first use setlocale with LC_COLLATE to set locale information if needed.

  usort($your_data,function($a,$b){
    setlocale (LC_COLLATE, 'pl_PL.UTF-8'); // Example of Polish language collation
    return strcoll($a->name,$b->name);
  });
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
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.