Skip to content

Instantly share code, notes, and snippets.

@da-h
Last active January 14, 2019 08:27
Show Gist options
  • Save da-h/73bef1dd25bee1715b32935d889b4a98 to your computer and use it in GitHub Desktop.
Save da-h/73bef1dd25bee1715b32935d889b4a98 to your computer and use it in GitHub Desktop.
Human Sorting
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