Why do numeric string comparisons give unexpected results?

后端 未结 4 1544
生来不讨喜
生来不讨喜 2021-01-15 15:59
\'10:\' < \'1:\'
# => true

Can someone explain me why the result in the above example is true? If I just compare \'1:\' and \'2:\' I get the

4条回答
  •  -上瘾入骨i
    2021-01-15 16:25

    It is character by character comparison in ASCII.

    '10:' < '1:' is (49 < 49) || (48 < 58) || (58 < ?)

    #=> true
    

    '1:' < '2:' is (49 < 50) || (58 < 58)

    #=> true
    

    Left to Right boolean check is used and check breaks where true is found.

    Note: It is just my observation over various example patterns.

提交回复
热议问题