?

Log in

No account? Create an account

PHP ordering: "<" and ">"

« previous entry | next entry »
Jun. 5th, 2006 | 10:46 am
mood: curiouscurious
music: Eraser - Thom Yorke

A very useful property for an ordering to have is for it to be consistent (transitivity in maths). For example, if a < b and b < c, then a < c. Orderings without this property can be quite unintuitive, and sorting no longer makes much sense.

Now, let's look at how PHP orders strings.

If both strings being compared can be parsed as integers or as floats, then they will be converted before comparison. Rather useful for when you have numeric values stored in strings.

If either of the strings cannot be parsed as numbers, then they will be compared as strings using the usual string ordering. The first characters are compared, then the second, and so on until a difference is found. Longer strings are preferred over shorter strings as a tie-breaker.

Here are some examples:

"2" < "10" - integer comparison
"10" < "10a" - string comparison. Longer string wins.
"10a" < "2" - string comparison. "2" wins because only the first characters are compared in this case.

But we have just demonstrated that a < b, b < c and c < a. PHP's comparison operator on strings is not consistent. This means that it should never be used for sorting, unless your input is restricted to numbers only, or non-numeric strings only.

A solution is to use strcmp(), which forces a string comparison, which is consistent.

Demonstration code:
if ("2" < "10") print "2 < 10\n";
if ("10" < "10a") print "10 < 10a\n";
if ("10a" < "2") print "10a < 2\n";

Link | Leave a comment | Share

Comments {5}

that condition is "transitivity"

from: anonymous
date: Jun. 7th, 2006 10:09 am (UTC)
Link

ie the operation is said to be transitive if a<b, b<c implies a<c I see that this seems to be "consistent" in the sense that you want the ordering to only derive things that you "know" to be true. So PHP ordering of strings is not transitive, and as you say, therefore unhelpful, specially if you don't know it and assume it is!! So I'm racking my brains for a case where a non-transitive operation is useful! How about the operation "is on talking terms with". Alan "is on talking terms with" Brian, Brian "is on talking terms with" Caleb but Alan is not "on talking terms with" Caleb might be a perfectly reasonable situation. Seems consistent to me!!!! Paula the pedant strikes again, Mum

Reply | Thread

btherl

Re: that condition is "transitivity"

from: btherl
date: Jun. 7th, 2006 12:41 pm (UTC)
Link

I know.. but no-one will understand it if I use that word :)

Reply | Parent | Thread

btherl

Re: that condition is "transitivity"

from: btherl
date: Jun. 7th, 2006 12:43 pm (UTC)
Link

What I mean is, no-one except a mathematician will understand :)

Reply | Parent | Thread

Re: that condition is "transitivity"

from: anonymous
date: Jun. 8th, 2006 12:50 pm (UTC)
Link

Fair enough (sigh)

Reply | Parent | Thread

Re: that condition is "transitivity"

from: anonymous
date: Jun. 8th, 2006 12:51 pm (UTC)
Link

I should say - Fair enough (The Outcast)

Reply | Parent | Thread