Created
July 1, 2015 17:54
-
-
Save limed/473a498641bbc7761a20 to your computer and use it in GitHub Desktop.
Sorts thing nicely in python
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
#!/usr/bin/python | |
def sort_nicely(l): | |
""" Sort the given list in the way that humans expect. | |
""" | |
convert = lambda text: int(text) if text.isdigit() else text | |
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] | |
l.sort( key=alphanum_key ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment