Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Natural sort of a string list
import re
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
# use
natural_sort(ll)
###################################################################################
import re
def natural_key(string_):
"""See http://www.codinghorror.com/blog/archives/001018.html"""
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)]
def natural_sort(str_list):
return sorted(str_list, key=natural_key)
# use
natural_sort(ll)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.