-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort items in rustdoc with integers in their names more naturally #39596
Comments
Hey guys, wanted to help with this, but this is the first time i look into rustdoc, any suggestion where i can start? |
When I took a look myself, it was just a call to |
Thanks @clarcharr any place in the code you can put here? I tried to search the whole project for |
I suspect the sorting is done here (https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render.rs#L1707), but I may be wrong. |
Yep thanks @keeperofdakeys looks like so, i will try to make changes around it. Thanks again ;) |
So, I found out a relatively easy way to do this type of sorting and decided to make a crate for it, which I've called version-sort. I'm not sure how we'd add this type of functionality to rustdoc specifically, but I figured that I'd mention that I have created the code that would do this sort of sorting. (heh) |
Hehe I think this functionality can be used for more than just in version number, but file name etc.., not sure how to call it though |
This is known as "natural sort order", and there are at least two crates for this, though I don't know about their quality. |
Notice that although for human-written code the suggested order would in general be better, it might be a regression if the "number" is not decimal (which might happen for example for machine-generated types) as |
@ranma42 Check out the crate I linked; I specifically track leading zeroes so that 001 comes before 01, before 1, before 10. |
Floating point strings don't sort by value (i.e. 2.2 < 2.10) which is why I'd consider this a version sort, not a numerical sort. For rustdoc this doesn't matter either way because periods can't be in identifiers. |
@clarcharr The issue is not with leading zeros, but with non-digit characters. For example, would EDIT: oops, I changed the example. in the original one, would |
Digits come before letters lexically, so, that'd still work as expected. Although I think that automatically coercing For example, |
Even though digits come before letters in the lexicographic order, this breaks when you group them. Specifically, the version-sort crate seem to tokenize the strings in numeric/non-numeric parts depending on decimal digits. This is generally useful, but it might not always be the correct choice. For example, according to version-sort, I agree that the comparison A better way to choose how the items should be sorted would involve looking at all of them together and trying to understand which patterns have been used to construct them, but this is obviously much more complicated than just comparing them in a pairwise fashion. |
My concern might actually not be much of a problem in practice (especially if it only affects auto-generated code, as that will often miss documentation). So far, the main example of documentation that would be affected by this change (that I know of) are the 8/16/32/64 types in the std docs; they would certainly benefit from being sorted naturally. |
If this is only for
This would mean |
@keeperofdakeys yes, your ruleset would definitely work (at least it would cover both my examples and the ones by the OP), assuming that it is applied to all the items as a whole. Otherwise you would end up using two different comparison functions when sorting |
@keeperofdakeys I like this idea! It would also make the sorting substantially easier to implement:
|
Fix for rust-lang#39596: sort Trait2 before Trait10. This is a change discussed in rust-lang#39596. Essentially, item names will be sorted as if they're (&str, u64) pairs instead of just `&str`, meaning that `"Apple" < "Banana"` and also `"Fruit10" > "Fruit2"`. Sample sorting: 1. Apple 2. Banana 3. Fruit 4. Fruit0 5. Fruit00 6. Fruit1 7. Fruit01 8. Fruit2 9. Fruit02 10. Fruit20 11. Fruit100 12. Pear Examples of generated documentation: https://docs.charr.xyz/before-doc/test_sorting/ https://docs.charr.xyz/after-doc/test_sorting/ Screenshots of generated documentation: Before: http://imgur.com/Ktb10ti After: http://imgur.com/CZJjqIN
Seems this issue has been fixed. Closing it. |
Right now,
Trait10
will be shown beforeTrait2
in rustdoc. It'd be nice if this could be changed to sort numbers in order by value whenever they're encountered.The text was updated successfully, but these errors were encountered: