aeonsky Posted January 1, 2008 Share Posted January 1, 2008 Hello, I'm back! When I get an array of directory contents, all folders are numbers. First folder is "004" and the second folder is "055". When I print the array, it puts 004 first and then 055. But if I make the folder "4" and "055", it puts the "055" first, why? Thanks very much for the time and consideration! Quote Link to comment https://forums.phpfreaks.com/topic/83941-solved-php-array-sort/ Share on other sites More sharing options...
asmith Posted January 1, 2008 Share Posted January 1, 2008 i havn't worked with array sort much , but as you are saying it echo 055 first than 4 . i guess it is because of the starting digits , have you ever seen this kind of sorting : 0 1 10 11 12 12 13 2 3 4 5 6 7 8 9 can you get the point ? although 10 11 12 ... is more than 9 but they are sort first , in other words it won't check what number is bigger, it check the first digit of a number ,then goes to second . that's why 4 is bigger than 055 , cause it begins with 4, but 055 begin with 0 . i hope this is what you were asking . Quote Link to comment https://forums.phpfreaks.com/topic/83941-solved-php-array-sort/#findComment-427168 Share on other sites More sharing options...
aeonsky Posted January 1, 2008 Author Share Posted January 1, 2008 Hmmm, that makes a lot of sense now. I'm just coding on a local server and Windows sorts it by value, but I checked now, cPanel sorts it the same way PHP sorts it. So now it means I have sprintf("%03d") every single directory now? O.o Oh well... Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/83941-solved-php-array-sort/#findComment-427169 Share on other sites More sharing options...
PHP_PhREEEk Posted January 1, 2008 Share Posted January 1, 2008 Please check into natsort(), which attempts to sort an array the way a 'human' would attempt to sort it... examples given at the link provided. There are many other sorting options available on the left hand nav of the page. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83941-solved-php-array-sort/#findComment-427195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.