jack jack -4 years ago 111
Python Question

How can I sort in python in ascending order?

I'm trying to sort values in a list but

y = [['22.344'], ['333.6575'], ['1.45'], ['122.56678'], ['33.768']]


when i use this command

y.sort(key=lambda x: x[0])


it doesn't sort the values in correct ascending values

[['1.45'], ['122.56678'], ['22.344'], ['33.768'], ['333.6575']]


How can i sort it in ascending order?

Answer Source

It's doing lexicographical sorting.

Try instead:

y.sort(key=lambda x: float(x[0]))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download