Tyson
Willy
Zoology
luke
mc2
oggy
test
case insensitive sort()
Moderator: General Moderators
case insensitive sort()
Is there any case-insensitive way to sort arrays? I keep getting this order with sort()
You can define your own sort function by doing:
Code: Select all
function mysort($a, $b) {
return strcmp(strtolower($a), strtolower($b));
}
usort($myarray, "mysort");
Last edited by TheMoose on Wed Jun 14, 2006 5:30 pm, edited 2 times in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
natcasesort(), or shockers of shockers: usort()
I have tried both of them and still come up with:feyd wrote:natcasesort(), or shockers of shockers: usort()
Code: Select all
function mysort($a, $b) {
return strcmp(strtolower($a), strtolower($b));
}
usort($directories, "mysort");
/* Both give me this:
This is a folder
Zoology
luke
mc2
test*/EDIT: nope... I print_r'd the array RIGHT after I did the compare and still get the wrong order... hmm...
EDIT AGAIN: OK geeze!! i finally found out that there were some arrays in my array (that i didn't even notice when I print_r'd) that were throwing things off... I need more coffee I guess.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US