Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavgeek/d6811a1c77f7d5b9da5ab3a92251dadb to your computer and use it in GitHub Desktop.
Save vaibhavgeek/d6811a1c77f7d5b9da5ab3a92251dadb to your computer and use it in GitHub Desktop.
Sort Accorfing to string and integer type in Python
test_files = os.listdir(f'{PATH}/test')
import re
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
return [ atoi(c) for c in re.split('(\d+)', text) ]
test_files.sort(key=natural_keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment