note 75611 added to function.sort
| From: | bluej100 at gmail | Date: | Thu, 07 Jun 2007 22:05:14 +0000 |
| Subject: | note 75611 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127282@lists.php.net to get a copy of this message | ||
One more solution for multidimensional sort: variable functions.
<?php
// $sort used as variable function--can be natcasesort, for example
function sort2d( &$arrIn, $index = null, $sort = 'sort') {
// pseudo-secure--never allow user input into $sort
if (strpos($sort, 'sort') === false) {$sort = 'sort';}
$arrTemp = Array();
$arrOut = Array();
foreach ( $arrIn as $key=>$value ) {
reset($value);
$arrTemp[$key] = is_null($index) ? current($value) : $value[$index];
}
$sort($arrTemp);
foreach ( $arrTemp as $key=>$value ) {
$arrOut[$key] = $arrIn[$key];
}
$arrIn = $arrOut;
}
?>
It appears to me that there are only two algorithms being proposed here (several times each):
1) copy into temp, pass temp to sort function, re-order by temp
2) implement search function in PHP
I'm curious whether anyone's implementation of (2) can beat (1) for speed. Someone have a
fast PHP mergesort they can benchmark against this one? Obviously, the fact that the (1) solutions
use at least two, possibly three times the memory is a drawback, but I expect that for most of us,
speed is significantly more important than memory. Maybe I'll check it myself when I have a
minute.
----
Server IP: 64.71.164.2
Probable Submitter: 63.253.43.10
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- https://master.php.net/note/edit/75611
Del: integrated -- https://master.php.net/note/delete/75611/integrated
Del: useless -- https://master.php.net/note/delete/75611/useless
Del: bad code -- https://master.php.net/note/delete/75611/bad+code
Del: spam -- https://master.php.net/note/delete/75611/spam
Del: non-english -- https://master.php.net/note/delete/75611/non-english
Del: in docs -- https://master.php.net/note/delete/75611/in+docs
Del: other reasons-- https://master.php.net/note/delete/75611
Reject -- https://master.php.net/note/reject/75611
Search -- https://master.php.net/manage/user-notes.php