0

there is list

a = ["100", "2", "33", "-10"]

when I apply sort like this

sorted(a) 

It gets

['-10', '100', '2', '33']

My question is how it sort numbers in string format?

I don't need any code which convert string to integers.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Hiba Rehman
  • 127
  • 2
  • 9

1 Answers1

0

It sorts by the first character of them, so 100's first character is 1 so it is gonna be before 33 and 2. Whereas the reason why -10 is before 100 is because in the python character order - is before the numbers:

>>> ord('-')
45
>>> ord('1')
49
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114