FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » sort array of objects by muliple values
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: sort array of objects by muliple values [message #172030 is a reply to message #172029] Thu, 27 January 2011 23:11 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On 27/01/11 21:38, Max wrote:
> On Jan 27, 10:27 pm, Denis McMahon <denis.m.f.mcma...@googlemail.com>
> wrote:

>> <?php
>>
>> class User {
>> private $firstname;
>> private $surname;
>> private $birthday;
>>
>> function __construct($firstname, $surname, $birthday) {
>> $this->firstname = $firstname;
>> $this->surname = $surname;
>> $this->birthday = $birthday;
>> }
>> function __get($n) {
>> return $this->$n;
>> }
>>
>> }
>>
>> function cmp_User($a, $b)
>> {
>> if ($a->birthday == $b->birthday) {
>> if ($a->surname == $b->surname) {
>> return 0;
>> }
>> return ($a->surname < $b->surname) ? -1 : 1;
>> }
>> return ($a->birthday < $b->birthday) ? -1 : 1;
>>
>> }
>>
>> $a = array (
>> new User('Alice', 'Smith', '1960-01-01'),
>> new User('Michael', 'Jordan', '1950-01-01'),
>> new User('Kevin', 'Dilan', '1950-01-01'),
>> );
>>
>> print "Before Sort\n===========\n";
>>
>> foreach ($a as $key => $value) {
>> print "Item $key; Name: {$value->surname}, {$value->firstname}; DOB:
>> {$value->birthday}\n";
>>
>> }
>>
>> usort($a, "cmp_User");
>>
>> print "\nAfter Sort\n==========\n";
>>
>> foreach ($a as $key => $value) {
>> print "Item $key; Name: {$value->surname}, {$value->firstname}; DOB:
>> {$value->birthday}\n";
>>
>> }
>>
>> ?>
>>
>> generates:
>>
>> Before Sort
>> ===========
>> Item 0; Name: Smith, Alice; DOB: 1960-01-01
>> Item 1; Name: Jordan, Michael; DOB: 1950-01-01
>> Item 2; Name: Dilan, Kevin; DOB: 1950-01-01
>>
>> After Sort
>> ==========
>> Item 0; Name: Dilan, Kevin; DOB: 1950-01-01
>> Item 1; Name: Jordan, Michael; DOB: 1950-01-01
>> Item 2; Name: Smith, Alice; DOB: 1960-01-01
>>
>> Rgds
>>
>> Denis McMahon
>
> Thanks for example! What if sort direction and/or fields only known at
> runtime?

Then your callback function becomes a bit more complex.

You could define the sort properties order and direction before calling
the sort function, and then use that in the sort:

<?php

class User {
private $firstname;
private $surname;
private $birthday;

function __construct($firstname, $surname, $birthday) {
$this->firstname = $firstname;
$this->surname = $surname;
$this->birthday = $birthday;
}
function __get($n) {
return $this->$n;
}
}

$sortparams = array("birthday" => "desc", "surname" => "asc",
"firstname" => "asc");

function cmp_User($a, $b) {
global $sortparams;
foreach ($sortparams as $member => $dir) {
if ($a->$member == $b->$member) continue;
if ($dir == "asc") {
return ($a->$member < $b->$member) ? -1 : 1;
}
else if ($dir == "desc") {
return ($b->$member < $a->$member) ? -1 : 1;
}
}
return 0;
}

$a = array (
new User('Michael', 'Jordan', '1950-01-01'),
new User('Alice', 'Smith', '1960-01-01'),
new User('Kevin', 'Dilan', '1950-01-01'),
new User('Fred', 'Jordan', '1950-01-01'),
new User('John', 'Doe', '1960-01-01'),
new User('Pete', 'Jones', '1960-01-01'),
);

print "Before Sort\n===========\n";

foreach ($a as $key => $value) {
print "Item $key; Name: {$value->surname}, {$value->firstname}; DOB:
{$value->birthday}\n";
}

usort($a, "cmp_User");

print "\nAfter Sort\n==========\n";

foreach ($a as $key => $value) {
print "Item $key; Name: {$value->surname}, {$value->firstname}; DOB:
{$value->birthday}\n";
}

?>

Generates:

Before Sort
===========
Item 0; Name: Jordan, Michael; DOB: 1950-01-01
Item 1; Name: Smith, Alice; DOB: 1960-01-01
Item 2; Name: Dilan, Kevin; DOB: 1950-01-01
Item 3; Name: Jordan, Fred; DOB: 1950-01-01
Item 4; Name: Doe, John; DOB: 1960-01-01
Item 5; Name: Jones, Pete; DOB: 1960-01-01

After Sort
==========
Item 0; Name: Doe, John; DOB: 1960-01-01
Item 1; Name: Jones, Pete; DOB: 1960-01-01
Item 2; Name: Smith, Alice; DOB: 1960-01-01
Item 3; Name: Dilan, Kevin; DOB: 1950-01-01
Item 4; Name: Jordan, Fred; DOB: 1950-01-01
Item 5; Name: Jordan, Michael; DOB: 1950-01-01
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: SNMPv3 for PHP?
Next Topic: Using server to list CSS page list in menu
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 05:05:22 GMT 2024

Total time taken to generate the page: 0.07377 seconds