Python: sorting string numbers not lexicographically

前端 未结 5 1039
猫巷女王i
猫巷女王i 2020-11-29 14:13

I have an array of string-numbers, like:

numbers = [\'10\', \'8\', \'918\', \'101010\']

When I use sorted(numbers), I get them

5条回答
  •  伪装坚强ぢ
    2020-11-29 14:39

    You can transform the elements of the array to integers by using the built-in map function.

    print sorted(map(int, numbers))
    

    Output:

    [8, 10, 918, 101010]
    

提交回复
热议问题