Created
December 2, 2019 16:06
-
-
Save YaYaB/6c7c6254338c26a78b7aea8437c86e7c to your computer and use it in GitHub Desktop.
Sort a list of string in an alphanumerical way
This file contains hidden or 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 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