Can anybody please tell me why the string comparisons below deliver these results?
>>\"1040\"<=\"12000\"
True
>> \"1040\"<=\"1
how about making them the same length?
That would unify numbers and alphas
1040 becomes 01040
01040 < 12000 now it makes sense
maybe that is why he felt it was wrong to compare strings of different length when the strings are numbers they should be the same length
Think alphabetized.
You're experiencing lexicographical ordering.
There are some generalized algorithms for this ordering in the book Elements of Programming. Search for the word lexicographical
.
To expand on the John P's answer, think of the strings as words, and read them left-to-right.
To look at it another way,
BAEA would come before BCAAA but after BAAAA
The strings are compared, one character at a time, from left to right:
10000
1040
12000
There's nothing wrong with comparing strings of different lengths.