-1

We have an array and need to sort the above array

Array
(
    [0] => stdClass Object
        (
            [id] => 229
            [firstname] => ggg
            [lastname] => fff
        )

    [1] => stdClass Object
        (
            [id] => 230
            [firstname] => aaa
            [lastname] => jjj
        )

)

I want to sort the array as (Sort by firstname)

Array
(
    [0] => stdClass Object
        (
            [id] => 230 
            [firstname] => aaa
            [lastname] => jjj

        )

    [1] => stdClass Object
        (
            [id] => 229
            [name] => ggg
            [lastname] => fff
        )

)
xkeshav
  • 53,360
  • 44
  • 177
  • 245
Sateesh
  • 11
  • 1
  • 2

1 Answers1

3

Use usort:

usort($ar, function($a, $b) {
  return strcmp($a->firstname, $b->firstname);
});
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 1
    +1, and in case of PHP < 5.3 use `usort($ar, create_function('$a,$b', 'return strcmp($a->firstname, $b->firstname);'));`. Or declare a function with a name use it. – Paul Aug 30 '11 at 07:17