Case Insensitive sort

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Case Insensitive sort

Post by Chris Corbyn »

Hi,

Any way to sort an array without putting DJ above Dead etc. Doble caps seem to always go to the top of the list.

Looked in manual but didn't spot anything, i could have missed it though.

Thanks
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could sort them numerically, like
sort($array, SORT_NUMERIC);
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

SORT_NUMERIC is no good because I need my list returned alphabetically.

strtolower looks good though. The only problem is that I need the results of the sort echoed back onto the page in their original format.

It's filenames in a big list so I want to display only the original way they are written even if the sort function sorts them as if they are lower case.

I'm sure that can't be too difficult to do though.

Thanks mate!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Nice thanks!

I did it like this ($file is each file in a directory)

Code: Select all

$tonefile = substr($file, 0, -4);

$lowercase = strtolower($file);
$ToneArray[$tonefile] = $lowercase;

asort($ToneArray);
foreach ($ToneArray as $n => $v) {
echo ' '.$n.' <br>';
}
Or something to that effect. I didn't copy and paste it so if there are errors in that code above there's no need to tell me.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or just:

Code: Select all

usort($ToneArray, create_function( '$a,$b', 'return strcasecmp($a,$b);' ) )
Post Reply