Last active
January 14, 2019 08:27
-
-
Save da-h/73bef1dd25bee1715b32935d889b4a98 to your computer and use it in GitHub Desktop.
Human Sorting
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
import re | |
def natural_keys(text): | |
return [ int(c) if c.isdigit() else c for c in re.split('(\d+)',text) ] | |
# example | |
my_list =['Hello1', 'Hello12', 'Hello29', 'Hello2', 'Hello17', 'Hello25'] | |
my_list.sort(key=natural_keys) | |
print(my_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment