Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

covertsurfer

macrumors 6502a
Original poster
Jan 18, 2007
579
7
I have an array 1,10,2,3,4,5,6,7,8,9

I want to sort it so I can return 1,2,3,4,5,6,7,8,9,10

I have tried the PHP function sort but it returns it in the original format.

Please help
 
My guess (I'm no PHP guru) is that it is sorting it properly.

1, 10, 2, 3, 4, 5, 6, 7, 8, 9 is already sorted... just not numerically.

Look into the PHP sort flags to have it sort numerically instead of "regular".

Basically it is sorting the 10 after the 1 since they both start with "1". If you look at the array with alpha replacements you would get something like:

B, BA, C, D, E, F, G, H, I, J
 
I don't think your code above works. Again I don't know PHP well but I do code. The API says sort returns a boolean. It looks like it sorts the array in place. So there is no need to send the value to $newarray since it actually sorts $array.

I could be wrong though.

Edit: Ah never mind, looks like you changed your code... :)
 
natsort doesn't change the keys though. Although maybe the OP doesn't need them changed. natsort would confuse me when I tried looping through the array I think... unless I was using it more like a key/value map.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.