Skip to content

Instantly share code, notes, and snippets.

@ArthurDelannoyazerty
Last active June 27, 2024 07:36
Show Gist options
  • Save ArthurDelannoyazerty/01525649bb7011825bdf34ca585214a7 to your computer and use it in GitHub Desktop.
Save ArthurDelannoyazerty/01525649bb7011825bdf34ca585214a7 to your computer and use it in GitHub Desktop.
Code that gives a sorted file list
files = [f for f in os.listdir(gpx_folderpath) if os.path.isfile(os.path.join(gpx_folderpath, f))]
def sort_key(file):
num = re.search(r'(\d+)', file).group(1)
num = num.replace('-', ' ')
num = int(num) if num.isdigit() else 0
return (num, file)
files.sort(key=sort_key)
@ArthurDelannoyazerty
Copy link
Author

ArthurDelannoyazerty commented Jun 25, 2024

With tthe line files.sort(key=sort_key)

['1.gpx',
 '2.gpx',
 '3.gpx',
 '4.gpx',
 '5.gpx',
 '6.gpx',
 '7.gpx',
 '8-9.gpx',
 '10.gpx',           <---
 '11.gpx']           <---

Without tthe line files.sort(key=sort_key)

['1.gpx',
 '10.gpx',         <---
 '11.gpx',         <---
 '2.gpx',
 '3.gpx',
 '4.gpx',
 '5.gpx',
 '6.gpx',
 '7.gpx',
 '8-9.gpx']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment