View Full Version : natsort() issue - arranges incorrectly
If I have an array:
$myArray = array('1','1A','10','10A','Apple','Apple Orange');
and I do a natsort($myArray) the results are "incorrect" (I say incorrect because, for what I need them for they are not correct; I am sure they are as intended in the PHP scheme of things).
For example, the natsort above would order the array:
I need it arranged in standard alphabetical:
I see why I get the results I do - php arranges slightly differently - but I don't know how to fix it.
Any help is appreciated.
07-12-2005, 11:14 PM
This isnt an answer for you.. but
IS in fact standard alphabetic... because.. it goes by numbers first.. hence any with 1 appears first.. then any with the 0 because 10 and 1A.. 0 is before A.
Did that make sense? lol
Why would it commence ordering by numbers and then jump to letters, and then back to numbers? :)
This is true... But the way a human would sort that list, at least the way I would (maybe that is saying something?) is the way of the second example.
My logic would be: 1A, a variant of 1, is before 10 or 10A, because 1 as a number comes before 10. But that is just a human way of doing it. Hence the following:
I updated my original post to use natsort() rather than sort() because that makes some headway (aranging in natural order seems to be a good start), but there is still a small issue that you touched upon. "A B" before "A" because spaces are before letters, in the same fashion that Taylor mentioned.
Fixing this is not as big of a deal now, but it is one of those minor annoyances that I'd like to fix if possible.
07-12-2005, 11:28 PM
Sorry but no lol... most humans would not sort it the way you do lol.. I don't mean that rude *hehe*... but the universal ordering is numberic before alpha.
It first equates the first character of each entry... ordering all options according to the first charatcer from numeric 0-9 then alpha A-Z.
Then, when 2 entries have the same 1st character.. it seperates this order by ordering of the 2nd character from numeric 0-9 then alpha A-Z.
I'm making myself dizzy.. lol.. but thats the way it continues on.
Like I mentioned before.. why would order your first character by numbers.. placing 1 above A (for Apple) and then suddenly change to A ahead of 0?
NOTE: When reading it does not take into take into account the number "10" - it does not read "10" it reads 1 and 0.
No offense taken. But that is exactly it! If you were making a list, would you put 1A infront of 10A, because a human would see 10 as 'ten', not 'one' and 'zero'.
Think aisles of a grocery store, or something.
And for the part about the spaces. Looking at that, if I were to, say, make a list of children in my class (I'm not a teacher, but just pretend), I would put "Apple" before "Apple Orange". Weird last names, but you get the point.
I still insist that if you asked an average person to order the list, they would place 1A before 10A and Apple before Apple Orange. I know this isn't how php sees it, because it moves one character at a time, rather than keeping a running tally until the type changes from numeric to alphabetic.
In the end, my research has brought me to usort($myArray, 'strnatcmp');. That solves the problem. And if you read php.net's description of all the ...nat... function varieties, they explain that it sorts them naturally as a human does. So I'm not completely weird! lol
FYI, usort($myArray, 'strnatcmp'); yields:
Good enough for me! haha I can get over the spaces issue. That's probably all in my head...
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.