[PHP] Re: Custom Usort Function

2006-01-24 Thread Matt Palermo
Any ideas on how to accomplish this?


Matt Palermo [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 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 

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



Re: [PHP] Re: Custom Usort Function

2006-01-24 Thread David Grant
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 [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 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



[PHP] Re: Custom Usort Function

2006-01-24 Thread Al

Matt Palermo wrote:
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



Did you try natcasesort()?  Seems like I recall, it's pretty close to what you 
need.

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