Javascript compare numbers as strings

前端 未结 2 972
醉话见心
醉话见心 2021-01-23 09:04

I wish to compare two strings in javascript. I am using localeCompare method but the output is not as expected

116457 < 3085
false

\"116457\" < \"3085\"
         


        
2条回答
  •  花落未央
    2021-01-23 10:08

    If 116457 were a word, it would come before 3085 in a dictionary.

    Consider a dictionary with

    • "applicative" (a long word starting with "a", c.f. a long digit string starting with "1")
    • "copy" (a short word starting with "c", c.f. a shorter digit string starting with "3").

提交回复
热议问题