Re: sort array of objects by muliple values [message #172019 is a reply to message #172016] |
Thu, 27 January 2011 19:59 |
Max
Messages: 4 Registered: January 2011
Karma:
|
Junior Member |
|
|
On Jan 27, 8:10 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/27/2011 2:04 PM, Max wrote:
>
>
>
>
>
>
>
>
>
>> Is there a simple way to sort $array below like SQL it does? I.e.
>> ORDER BY birthday ASC, name ASC
>> Output would be Kevin, Michael, Alice
>
>> Thanks!
>
>> <?php
>> class User {
>> private $firstname;
>> private $surname;
>> private $birthday;
>
>> function __construct($firstname, $surname, $birthday) {
>> $this->firstname = $name;
>> $this->surname = $surname;
>> $this->birthday = $birthday;
>> }
>> function __get($n) {
>> return $this->$n;
>> }
>> }
>
>> $array = array (
>> new User('Alice', 'Smith', '1960-01-01'),
>> new User('Michael', 'Jordan', '1950-01-01'),
>> new User('Kevin', 'Dilan', '1950-01-01'),
>> );
>
> Use usort() with a static member function. See
>
> http://us2.php.net/manual/en/function.usort.php
>
> for an example.
it sorts only by one field
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
|
|
|