Last active
February 16, 2017 02:46
-
-
Save whoiscarlo/0dd7f65d765491feabeb to your computer and use it in GitHub Desktop.
Natural Sort
This file contains hidden or 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
| import re | |
| def sorted_nicely(l): | |
| """ Sort the given iterable 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) ] | |
| return sorted(l, key = alphanum_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment