sphinx
sphinx copied to clipboard
toctree globbing uses lexicographical instead of natural sorting
When using a toctree with the glob option, the filenames in the list are sorted using Python's sorted(), which does lexicographical sorting.
An example result would be:
- file10.rst
- file2.rst
- file8.rst
This is because in lexicographical sorting 1 < 2, hence item10 will come before item2.
The sorting differs from what operating systems typically do, which would be:
- file2.rst
- file8.rst
- file10.rst
This order makes more sense for humans.
The easiest way to implement this would be to add a new option to the toctree directive (e.g. :natsort:) that replaces sorted() with natsorted() from the Natsorted module. Just changing the behavior without an option is possible but will generate a different order for some document.
I can make a PR for this, but only if there are no objections that:
- An additional pip requirement is added
- This only applies to globbed filenames, not actual document titles (see #1430).
Would be a nice addition, for things like release notes, where one might want to have one .rst file per version, right now I'm listing the files manually in the natural order for that.
+1: Reasonable.
Is there any objection to moving forward with this? It would a useful improvement for my team too.