sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

toctree globbing uses lexicographical instead of natural sorting

Open jessetan opened this issue 8 years ago • 3 comments

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:

  1. An additional pip requirement is added
  2. This only applies to globbed filenames, not actual document titles (see #1430).

jessetan avatar May 30 '17 08:05 jessetan

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.

Sarcasm avatar Aug 21 '19 08:08 Sarcasm

+1: Reasonable.

tk0miya avatar Jul 14 '20 16:07 tk0miya

Is there any objection to moving forward with this? It would a useful improvement for my team too.

chadnorvell avatar Jan 29 '24 20:01 chadnorvell