note 21890 deleted from function.sort by derick
| From: | derick@php.net | Date: | Wed, 31 Aug 2005 10:38:37 +0000 |
| Subject: | note 21890 deleted from function.sort by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94497@lists.php.net to get a copy of this message | ||
Note Submitter: rob at pulpchat dot com
----
Here is a simple, CASE-INSENSITIVE sort function
that allows you to specify the starting point and/or
ending point in an array. This can be quite useful
if you want to leave the first n elements alone, etc.
function QSortLCase(&$xArray,$lower=0,$upper=0) {
//v06.15.01, PHP version 05.29.02
//xArray is passed by reference
$xLow=$lower;
if ($upper==0) {$upper=count($xArray)-1;}
$xHigh=$upper;
if ($upper <= $lower) {return;}
$midPoint = floor(($lower + $upper)/2);
$strTemp = strtolower($xArray[$midPoint]);
while ($xLow<=$xHigh) {
while (strtolower($xArray[$xLow])<$strTemp) {
$xLow++;
if ($xLow==$upper) {break;}
}
while ($strTemp<strtolower($xArray[$xHigh])) {
$xHigh--;
if ($xHigh==$lower) {break;}
}
if ($xLow<=$xHigh) {
$strBuffer=$xArray[$xLow];
$xArray[$xLow]=$xArray[$xHigh];
$xArray[$xHigh]=$strBuffer;
$xLow++;
$xHigh--;
}
}
if ($lower<$xHigh) {QSortLCase($xArray,$lower,$xHigh);}
if ($xLow<$upper) {QSortLCase($xArray,$xLow,$upper);}
}
Examples of using this function:
--------------------------------
QSortLCase($myArray); //sorts all elements of the array
QSortLCase($myArray,1); //ignores element 0, sorts the rest
QSortLCase($myArray,5,25); //only sorts elements 5 through 25
QSortLCase($myArray,7,8); //only sorts elements 7 and 8
I originally wrote this function in another language
because a good quick sort function was not available.
Although there are many ways to perform sorting in PHP,
I created this PHP version because it is the simplest way
I could find to do exactly what I needed. Hopefully it
will help someone.