note 12170 deleted from function.sort by derick
| From: | derick@php.net | Date: | Wed, 31 Aug 2005 10:36:18 +0000 |
| Subject: | note 12170 deleted from function.sort by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94490@lists.php.net to get a copy of this message | ||
Note Submitter: tim at sod dot co dot uk
----
I was looking for a way to sort an array alphabetically. The first post points out that
'Ab' is different to 'AB' and you can't make it case insensitive (well, I
couldn't). So I did it like this:<br><br>
function cmp ($a, $b) {
$tmp[0]=strtoupper($a);
$tmp[1]=strtoupper($b);
sort($tmp);
return (strcmp(strtoupper($tmp[1]) , strtoupper($b))) ? 1 : -1;
}
$listing[0]="AB";
$listing[1]="yzZAZz";
$listing[2]="YZzazZ";
$listing[3]="LhaASD";
$listing[4]="A";
$listing[5]="aB";
usort($listing, "cmp");
echo implode(",",$listing);
<br><br>
Which produces: <br>A,AB,aB,LhaASD,yzZAZz,YZzazZ