Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nobody sorts semantic versions correctly
#!/usr/bin/env python
import distutils.version
import pkg_resources
import random
import semantic_version
import sys
versions = sys.argv[1:]
if not versions:
versions = 'v1.0.0-alpha v1.0.0-alpha.1 v1.0.0-alpha.beta v1.0.0-beta v1.0.0-beta.2 v1.0.0-beta.11 v1.0.0-rc.1 v1.0 v1.0.0 v1.0.1 v1.0.2'.split()
def semver_key(version):
return semantic_version.Version(version.strip('v'))
except ValueError:
return semantic_version.Version(version.strip('v'), partial=True)
strings = sorted(versions)
distutils = sorted(versions, key=distutils.version.LooseVersion)
setuptools = sorted(versions, key=pkg_resources.parse_version)
semver = sorted(versions, key=semver_key)
fmt = '%-19s%-32s%-29s%s'
print fmt % ('str', 'distutils.version.LooseVersion', 'pkg_resources.parse_version', 'semantic_version.Version')
for lst in zip(strings, distutils, setuptools, semver):
print fmt % lst
Copy link

lukeyeager commented Oct 1, 2015

Compare with

1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment