Re: Custom Usort Function
| From: | Matt Palermo | Date: | Tue, 24 Jan 2006 14:40:57 +0000 |
| Subject: | Re: Custom Usort Function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-229167@lists.php.net to get a copy of this message | ||
Any ideas on how to accomplish this?
""Matt Palermo"" <webmaster@sweetphp.com> wrote in message
news:C2.D0.06819.98A75D34@pb1.pair.com...
> I'm try to sort a list of items using a usort function. Basically, I want
> it to sort in this order:
>
> Numbers (0-9)
> Letters (Aa-Zz)
> Other (anything else)
>
> To explain a little further, I want anything that starts with a number to
> show up first in the sorted list, then anything that starts with a letter,
> and finally anything that starts with some other character. My usort
> function I'm using now gives me results close to this, but it sorts it as
> follows (which is wrong for what I need):
>
> Numbers (0-9)
> Other (anything else)
> Letters (Aa-Zz)
>
> They are all sorted properly alphabetically, but not in the group order
> that I need them in. Here is the usort function I'm using right now:
>
>
> function myUsort($x, $y)
> {
> if(strtolower($x) == strtolower($y)) return 0;
> if(strtolower($x) < strtolower($y)) return -1;
> return 1;
> }
>
> As I stated above, this provides me with the proper alphabetical results,
> but I need it tweaked. Basically this function will give a sorted list
> like this:
>
> 007
> 90210
> __underscore
> abra-cadabra
> Zebra
>
> But I need the function to provide a sorted list like this:
>
> 007
> 90210
> abra-cadabra
> Zebra
> __underscore
>
>
> Anyone know what I need to do to get these results? Please let me know.
>
> Thanks,
>
> Matt Palermo
> http://sweetphp.com