Skip to content

Instantly share code, notes, and snippets.

@YaYaB
Created December 2, 2019 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YaYaB/6c7c6254338c26a78b7aea8437c86e7c to your computer and use it in GitHub Desktop.
Save YaYaB/6c7c6254338c26a78b7aea8437c86e7c to your computer and use it in GitHub Desktop.
Sort a list of string in an alphanumerical way
import re
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
def sort_list(list_to_sort):
return list_to_sort.sort(key=natural_keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment