Page 1 of 1

case insensitive sort()

Posted: Wed Jun 14, 2006 5:08 pm
by Luke
Is there any case-insensitive way to sort arrays? I keep getting this order with sort()
Tyson
Willy
Zoology
luke
mc2
oggy
test

Posted: Wed Jun 14, 2006 5:27 pm
by TheMoose
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");

Posted: Wed Jun 14, 2006 5:28 pm
by feyd
natcasesort(), or shockers of shockers: usort()

Posted: Wed Jun 14, 2006 5:40 pm
by Luke
feyd wrote:natcasesort(), or shockers of shockers: usort()
I have tried both of them and still come up with:

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*/
Let me look at the rest of my code and see if maybe it is getting resorted somewhere else or something.... this is driving me crazy!

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.

Posted: Wed Jun 14, 2006 6:01 pm
by Christopher
How about:

Code: Select all

usort($directories, 'strcasecmp');