earnestt1234/seedir

Natsort algorithm not set to PATH

Closed this issue · 2 comments

The natsort algorithm is not set to natsort.PATH. This causes issues with sorting files and folders.

For example:

zenodo/
├─rational_dimension.ipynb
├─quasiperiod_svm_learning_curve_dim_4.py
├─quasiperiod_svm_learning_curve_dim_3.py
├─quasiperiod_svm_learning_curve_dim_2.py
├─quasiperiod_svm_hyperparameter_tuning.py
├─quasiperiod_log_svm_learning_curve_dim_10.py
├─quasiperiod_log_svm_learning_curve_dim_3.py
├─quasiperiod_log_svm_learning_curve_dim_2.py
├─quasiperiod_log_svm_hyperparameter_tuning.py
├─a_quasiperiod.m
├─README.txt
└─QUASIPERIOD.ipynb

More info: https://natsort.readthedocs.io/en/master/examples.html#sort-os-generated-paths

Relevant code:

if first in ['folders', 'files']:
folders = [p for p in items if os.path.isdir(p)]
files = [p for p in items if not os.path.isdir(p)]
folders = natsort.natsorted(folders, reverse=sort_reverse, key=key)
files = natsort.natsorted(files, reverse=sort_reverse, key=key)
return folders + files if first == 'folders' else files + folders
else:
return list(natsort.natsorted(items,
reverse=sort_reverse,
key=key))

I think I was wrong. I close this issue.

One tip for the reader who wants to set the algorithm explicitly: use sort_key=ns.natsort_keygen(alg=ns.PATH)

@J535D165 thanks for sharing this, I was not aware of the potential issues with paths and natsort. Your second comment looks like a great workaround.

The sorting I implemented for seedir only works off of the basename of each item - so it hopefully avoids any sorting issues due to parsing slashes and parent folders. But if there are other instances that would be remedied by adding natsort.PATH, I would probably be in favor of adding