Re: Re: Custom Usort Function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Matt,

Personally, I'd do a logic case for each combination, e.g.

if (ctype_alpha($x[0])) {
    if (ctype_alpha($y[0])) {
        //strcmp
    } elseif (ctype_digit([$y[0])) {
        //-1
    } else {
	// 1;
    }
} elseif (ctype_digit([$x[0])) {
    if (ctype_alpha($y[0])) {
        // 1;
    } elseif (ctype_digit([$y[0])) {
        // >
    } else {
	// 1;
    }
} else {
    if (ctype_alpha($y[0])) {
        // -1;
    } elseif (ctype_digit([$y[0])) {
        // -1
    } else {
	// strcmp
    }
}

It's not the prettiest, but it will work.

David

Matt Palermo wrote:
> Any ideas on how to accomplish this?
> 
> 
> ""Matt Palermo"" <webmaster@xxxxxxxxxxxx> wrote in message 
> news:C2.D0.06819.98A75D34@xxxxxxxxxxxxxxx
>> 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;
>> }

function myUsort($x, $y) {
if (ctype_digit($x[0])) {
} elseif (ctype_alpha($x[0]}
}

>> 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 
> 


-- 
David Grant
http://www.grant.org.uk/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux