A sorting question

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
nate_24
Forum Newbie
Posts: 2
Joined: Fri Nov 26, 2010 2:05 pm

A sorting question

Post by nate_24 »

Hey,

I am trying to sort an array sorting by numbers first and letters second.

For example: an array could look like $array = ('1', 'C9', 'E2', '2', 'C11', ... etc.);

I would like it to output in the order: 1, 2, C9, C11, E2. But right now the code I have outputs: C9, C11, E2, 1, 2. Any help is appreciated. Thanks!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: A sorting question

Post by McInfo »

Try natsort() or natcasesort().

It that isn't suitable, write your own comparison function and use usort().
nate_24
Forum Newbie
Posts: 2
Joined: Fri Nov 26, 2010 2:05 pm

Re: A sorting question

Post by nate_24 »

natsort() worked. Thank you for the suggestion.
Post Reply