Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-12-2005, 10:06 PM   PM User | #1
mtd
Regular Coder

 
Join Date: Jun 2003
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
mtd is an unknown quantity at this point
natsort() issue - arranges incorrectly

Hi!

If I have an array:
PHP Code:
$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:
1A
1
10A
10
Apple Orange
Apple

I need it arranged in standard alphabetical:
1
1A
10
10A
Apple
Apple Orange

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.

Last edited by mtd; 07-12-2005 at 10:15 PM..
mtd is offline   Reply With Quote
Old 07-12-2005, 10:14 PM   PM User | #2
Taylor_1978
Regular Coder

 
Join Date: Jun 2003
Location: Australia
Posts: 528
Thanks: 8
Thanked 8 Times in 8 Posts
Taylor_1978 is on a distinguished road
This isnt an answer for you.. but

1
10
10A
1A
Apple
Book

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?
Taylor_1978 is offline   Reply With Quote
Old 07-12-2005, 10:21 PM   PM User | #3
mtd
Regular Coder

 
Join Date: Jun 2003
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
mtd is an unknown quantity at this point
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.

Anyone?
mtd is offline   Reply With Quote
Old 07-12-2005, 10:28 PM   PM User | #4
Taylor_1978
Regular Coder

 
Join Date: Jun 2003
Location: Australia
Posts: 528
Thanks: 8
Thanked 8 Times in 8 Posts
Taylor_1978 is on a distinguished road
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.
Taylor_1978 is offline   Reply With Quote
Old 07-12-2005, 10:41 PM   PM User | #5
mtd
Regular Coder

 
Join Date: Jun 2003
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
mtd is an unknown quantity at this point
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:
1A
1
10A
10
Apple Orange
Apple

Good enough for me! haha I can get over the spaces issue. That's probably all in my head...

Last edited by mtd; 07-12-2005 at 11:08 PM..
mtd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:13 PM.


Advertisement
Log in to turn off these ads.