I have a Version class that contains a version_number string, and can obtain a sorted list of those strings like so:
sorted_versions = [v.version_number for v in self.version_set]
sorted_versions.sort(key=LooseVersion)
This works great!
sorted_versions: [u'1.6.3', u'1.7.0']
However, I would like to maintain a sorted list of the actual Version objects, not just their version_number properties, but I’m not sure how to accomplish this.
In my head, I would like to see something like this:
sorted_versions = self.version_set
sorted_versions.sort(key=LooseVersion, property=version_number)
Resulting in:
sorted_versions: [<Version: 1.6.3>, <Version: 1.7.0>]
How about:
Using
sorted()here to avoid changing the original list which is what your ‘would like to see’ code would do if it worked.