[Tutor] Quick Question on String Compare

Andreas Perstinger andipersti at gmail.com
Sat Jun 1 09:42:22 CEST 2013


On 01.06.2013 07:47, Sarma Tangirala wrote:
> I had a quick question on how string compare works. If did '1001' <= '999'
> I get true. I know how the string compare works but I was wondering why it
> were so. Why doesn't the string length factor into the comparison?

Because usually you are interested in the lexicographical order when you 
compare strings.
You wouldn't expect "pear" listed before "apple" in an ordinary 
dictionary, would you?

> For example, If I compared character-by-character but also found how
> different the lengths are, I could avoid a wrong logical answer as in
> the example above.

Why is it a "wrong logical answer"?
Neither '1001' nor '999' are numbers but strings.
If you want to compare them like numbers you need to convert them to 
numbers first.

Bye, Andreas


More information about the Tutor mailing list