0

I'm trying to use StrictVersion on the code below:

from distutils.version import StrictVersion
from operator import itemgetter

v = [{'ver': '1.1.12'},{'ver': '1.0.0'},{'ver': '1.3.3'},{'ver': '1.0.12'},{'ver': '1.0.2'}]
v.sort(key=itemgetter("ver"),reverse=True)

This is sorting based on version with below code but it's not sorting properly in above code.

versions = ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"]
versions.sort(key=StrictVersion,reverse=True)

How to use key=StrictVersion on above with key? Or is there an alternative?.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Vijaysinh Parmar
  • 897
  • 1
  • 15
  • 19

2 Answers2

2

all the keys are the same, how do you intend on sorting the keys when they all are the same?

for the version part you can sort using:

list_to_sort = ['1.1.4', '1.6.9', '1.1.2']
sorted(list_to_sort, key=lambda version: int(''.join(["%02X" % int(i) for i in version.split('.')]), 16))
2
v.sort(key = lambda x:StrictVersion( x['ver']),reverse=True)
[{'ver': '1.3.3'},
 {'ver': '1.1.12'},
 {'ver': '1.0.12'},
 {'ver': '1.0.2'},
 {'ver': '1.0.0'}]
Onyambu
  • 67,392
  • 3
  • 24
  • 53