note 21890 added to function.sort

From: Date: Thu, 30 May 2002 16:44:36 +0000
Subject: note 21890 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-31224@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/function.sort.php http://master.php.net/manage/user-notes.php?action=edit+21890 http://master.php.net/manage/user-notes.php?action=delete+21890 http://master.php.net/manage/user-notes.php?action=reject+21890

« previous php.notes (#31224) next »