Thanks all,
I managed to get the usort wrapper method I found working and adjusted to
uasort so I can retain the top level keys. Here it is if anyone is interested:
function orderBy($ary, $clause, $ascending = true) {
$clause = str_ireplace('order by', '', $clause);
$clause = preg_replace('/\s+/', ' ', $clause);
$keys = explode(',', $clause);
$dirMap = array('desc' => 1, 'asc' => -1);
$def = $ascending ? -1 : 1;
$keyAry = array();
$dirAry = array();
foreach($keys as $key) {
$key = explode(' ', trim($key));
$keyAry[] = trim($key[0]);
if(isset($key[1])) {
$dir = strtolower(trim($key[1]));
$dirAry[] = $dirMap[$dir] ? $dirMap[$dir] : $def;
} else {
$dirAry[] = $def;
}
}
$fnBody = '';
for($i = count($keyAry) - 1; $i >= 0; $i--) {
$k = $keyAry[$i];
$t = $dirAry[$i];
$f = -1 * $t;
$aStr = '$a[\''.$k.'\']';
$bStr = '$b[\''.$k.'\']';
if(strpos($k, '(') !== false) {
$aStr = '$a->'.$k;
$bStr = '$b->'.$k;
}
if($fnBody == '') {
$fnBody .= "if({$aStr} == {$bStr}) { return 0; }\n";
$fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
} else {
$fnBody = "if({$aStr} == {$bStr}) {\n" . $fnBody;
$fnBody .= "}\n";
$fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
}
}
if($fnBody) {
$sortFn = create_function('$a,$b', $fnBody);
uasort($ary, $sortFn);
}
return $ary;
}
// Usage
$testAry = array(
array('a' => 1, 'b' => 2, 'c' => 3),
array('a' => 2, 'b' => 1, 'c' => 3),
array('a' => 3, 'b' => 2, 'c' => 1),
array('a' => 1, 'b' => 3, 'c' => 2),
array('a' => 2, 'b' => 3, 'c' => 1),
array('a' => 3, 'b' => 1, 'c' => 2)
);
orderBy($testAry, 'a ASC, b DESC');
----- Original Message -----
From: Jochen Daum
To: [email protected]
Sent: Tuesday, July 28, 2009 9:14 AM
Subject: [phpug] Re: Sorting Multidimensional Array on values of multiple keys
Hi,
Can you paste how you used usort? I've used it fine many times and it is what
you need here.
Once I get to the office I can also paste you some code to parse an order by
clause.
Kind regards,
Jochen
Mobile: 021 567 853
Phone: 09 630 3425
Email: [email protected]
Skype: jochendaum
Web: www.automatem.co.nz
On Jul 28, 2009 9:09 AM, "Andrew McMurtrie" <[email protected]> wrote:
I know in vb6 I used to load something like this into an AODB recordset and
use the sort and filtering functions in that to manipulate complex data sets,
haven’t tried multidimensional sorts in PHP.
I was sure php had multidimensional sort (which might be you usort) does
ezcomponents or Zend have anything.
Andrew
From: [email protected] [mailto:[email protected]] On Behalf
Of Aaron Cooper
Sent: Tuesday, 28 July 2009 9:00 a.m.
To: [email protected]
Subject: [phpug] Sorting Multidimensional Array on values of multiple keys
Morning, A challenge. So I have this array, simple example as
follows: $testAry = a...
__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4282 (20090727) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com
__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4282 (20090727) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com
--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]
-~----------~----~----~----~------~----~------~--~---