0
votes

The string comparison "3" <= "4";; evaluates as "bool = true" Here 3 is less than 4 so this makes sense.

This string comparison "3" <= "9";;evaluates as "bool = true" 3 is less than 9 so this makes sense.

Why then does the string comparison "3" <= "10";; evaluate to "bool = false"? Does it have to do with the length of strings, or perhaps their ASCII values?

Thank you for your time.

1
you are comparing ASCII text with ASCII text, therefor "3" is greater than "1". No need to look any further than the first character. It is not being evaluated as a number.SteveFerg

1 Answers

8
votes

It's a normal lexicographical order.

"3" > "10" for the same reason that "d" > "ba".

The first character of string A is compared to the first character of string B. If they're different, you're done.

If they're the same, then the second character of string A is compared to the second character of string B. If they're different, you're done.

If they're the same, then the third character ...

This continues until either both strings run out of characters at the same time (then they're equal) or one of the strings runs out first (that string is "less than" the other one).