-3

I have a method that list some versions of an application on my computer, the list following has this format and the content are strings:

['1.15.1.23', '1.10.1.34', '1.9.2.3', ...]

When I call sorted(mylist), the output does not appear to sort some items, I'm getting this:

['1.15.1.51', '1.15.1.9', '1.15.2.11', '1.15.2.6']

What I'm doing wrong? I expected the output as:

['1.15.1.9', '1.15.1.51', '1.15.2.6', '1.15.2.11']
Renan Gomes
  • 771
  • 1
  • 15
  • 34

1 Answers1

5

Supply a key to the sorted function:

sorted(mylist, key=lambda v: map(int, v.split('.')))
a_guest
  • 34,165
  • 12
  • 64
  • 118
  • 1
    Did you try `mylist.sort(key=...)`? Because that sorts the list in-place and returns `None`. `sorted` definitely returns the result. – a_guest Jan 13 '17 at 13:18