Skip to content

Instantly share code, notes, and snippets.

@lukeyeager
Created September 30, 2015 21:39
Show Gist options
  • Save lukeyeager/9922a2229f7a03d1463c to your computer and use it in GitHub Desktop.
Save lukeyeager/9922a2229f7a03d1463c to your computer and use it in GitHub Desktop.
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()
random.shuffle(versions)
def semver_key(version):
try:
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
@lukeyeager
Copy link
Author

Compare with http://semver.org/:

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