0

Why Sorted() function shows '1000' less than '996', '999' etc.

Sample Code:

 pyList = ['1000', '999', '996', '997', '998']

 print(sorted(pyList))

I expect the output: ['996', '997', '998', '999', '1000'],

but the actual output is ['1000', '996', '997', '998', '999']

bharatk
  • 4,202
  • 5
  • 16
  • 30

1 Answers1

0

you can try:

sorted(pyList, key=int)

your elements are strings, and a string comparison is done from the first character to the last one

kederrac
  • 16,819
  • 6
  • 32
  • 55