-4

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
  • 664
  • 1
  • 13
  • 32

1 Answers1

5

Supply a key to the sorted function:

sorted(mylist, key=lambda v: map(int, v.split('.')))
a_guest
  • 30,279
  • 8
  • 49
  • 99
  • 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
  • The other solution does not work, using `sorted` (after yr edit) works like charm, thanks. – Renan Gomes Jan 13 '17 at 13:19