An array that needs to be sorted numerically

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
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

An array that needs to be sorted numerically

Post by lovelf »

Hi, I have an array that would like to sort numerically but it also contains strings, when sorted the following comes:

Code: Select all

9{},8{},7{},6{},5{},4{},3{},2{},1{},10{},11{}0{}
When it should be

Code: Select all

11{},10{},9{},8{},7{},6{},5{},4{},3{},2{},1{},0{}
The strings are mandatory for me on this array, it just starts with the right number to be sorted but the sort turns out to be bad, thanks. I'm using rsort($arri);
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: An array that needs to be sorted numerically

Post by tr0gd0rr »

you will probably need to use usort(), and perhaps strnatcasecmp() will work. If not, you'll need to do some regexes in your usort() callback.
Post Reply