1

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.

U12-Forward
  • 65,118
  • 12
  • 70
  • 89
Hiba Rehman
  • 111
  • 1
  • 6

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
>>> 
U12-Forward
  • 65,118
  • 12
  • 70
  • 89