note 69346 added to function.natsort
| From: | at gmailbereikme | Date: | Fri, 01 Sep 2006 15:06:39 +0000 |
| Subject: | note 69346 added to function.natsort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116586@lists.php.net to get a copy of this message | ||
Here's a handy function to sort an array on 1 or more columns using natural sort:
<?php
// Example: $records = columnSort($records, array('name', 'asc',
'addres', 'desc', 'city', 'asc'));
$globalMultisortVar = array();
function columnSort($recs, $cols) {
global $globalMultisortVar;
$globalMultisortVar = $cols;
usort($recs, 'multiStrnatcmp');
return($recs);
}
function multiStrnatcmp($a, $b) {
global $globalMultisortVar;
$cols = $globalMultisortVar;
$i = 0;
$result = 0;
while ($result == 0 && $i < count($cols)) {
$result = ($cols[$i + 1] == 'desc' ? strnatcmp($b[$cols[$i]], $a[$cols[$i]]) : $result =
strnatcmp($a[$cols[$i]], $b[$cols[$i]]));
$i+=2;
}
return $result;
}
?>
Greetings,
- John
----
Server IP: 83.137.20.120
Probable Submitter: 194.109.239.148
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.natsort.php
Edit -- https://master.php.net/note/edit/69346
Del: integrated -- https://master.php.net/note/delete/69346/integrated
Del: useless -- https://master.php.net/note/delete/69346/useless
Del: bad code -- https://master.php.net/note/delete/69346/bad+code
Del: spam -- https://master.php.net/note/delete/69346/spam
Del: non-english -- https://master.php.net/note/delete/69346/non-english
Del: in docs -- https://master.php.net/note/delete/69346/in+docs
Del: other reasons-- https://master.php.net/note/delete/69346
Reject -- https://master.php.net/note/reject/69346
Search -- https://master.php.net/manage/user-notes.php