Pavan Nath Pavan Nath - 4 years ago 120
Python Question

why sort in python is not working?

code:
list=['1','85863','432','93','549834']
list.sort()
print (list)

Actual output:
>>>
['1', '432', '549834', '85863', '93']
#why sort is not working
Expected output:
['1','93','432','83863','549834']


I have tried other sort operations also but they are displaying same output.
when i tried to read list from keyboard input they are reading only strings but not int please help me why?

Answer Source

You need to first convert the strings to int.

list = [int(ele) for ele in list]
list.sort()
print list
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download