Created
October 2, 2017 20:38
-
-
Save zahna/ee9c7d85400800b4873289213086813f to your computer and use it in GitHub Desktop.
optimized natural sorting key function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def alphanum_key(s): | |
'''http://nedbatchelder.com/blog/200712/human_sorting.html''' | |
tryint = lambda s: int(s) if s.isdigit() else s | |
return [ tryint(c) for c in re.split('(\d+)', s) ] | |
alist.sort(key=alphanum_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment