Skip to content

Instantly share code, notes, and snippets.

@mattwthompson
Last active September 4, 2018 21:31
Show Gist options
  • Save mattwthompson/eab1dd86e41082ca3e1b5ddfc3174bc7 to your computer and use it in GitHub Desktop.
Save mattwthompson/eab1dd86e41082ca3e1b5ddfc3174bc7 to your computer and use it in GitHub Desktop.
import re
def _atoi(text):
return int(text) if text.isdigit() else text
def natural_sort(text):
return [_atoi(a) for a in re.split(r'(\d+)', text)]
lst = ['mxene_001g', 'mxene_002', 'mxene_003', 'mxene_004', 'mxene_005', 'opls_753', 'opls_754', 'opls_755', 'opls_759']
sorted_lst = lst
sorted_lst.sort(key=natural_sort)
assert lst == sorted_lst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment