You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sort items in rustdoc with integers in their names more naturally #39596
Right now, Trait10 will be shown before Trait2 in rustdoc. It'd be nice if this could be changed to sort numbers in order by value whenever they're encountered.
When I took a look myself, it was just a call to slice::sort that did the sorting, so, if you make a slice sorting function that works and give it a few tests, someone should be able to point you to the place to call it.
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)
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 T000T010T0A0T110 would then not be considered sorted.
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 T100 sort before T1A0? (if you interpret the numbers in hexadecimal, i.e. as 0x100 and 0x1A0, this is the obvious sorting, but if you split at every non-digit, then it is not).
EDIT: oops, I changed the example. in the original one, would T010 be sorted before T0A0?
Digits come before letters lexically, so, that'd still work as expected. Although I think that automatically coercing a-f as numbers has the ability for too many false positives.
For example, "String1" < "StringArgument" < "String10" just doesn't make any sense.
Activity
phungleson commentedon Feb 11, 2017
Hey guys, wanted to help with this, but this is the first time i look into rustdoc, any suggestion where i can start?
clarfonthey commentedon Feb 11, 2017
When I took a look myself, it was just a call to
slice::sortthat did the sorting, so, if you make a slice sorting function that works and give it a few tests, someone should be able to point you to the place to call it.phungleson commentedon Feb 11, 2017
Thanks @clarcharr any place in the code you can put here?
I tried to search the whole project for
Trait10Trait2andslice::sortbut couldn't find anything.keeperofdakeys commentedon Feb 12, 2017
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.
phungleson commentedon Feb 12, 2017
Yep thanks @keeperofdakeys looks like so, i will try to make changes around it.
Thanks again ;)
clarfonthey commentedon Feb 20, 2017
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)
phungleson commentedon Feb 20, 2017
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
Boddlnagg commentedon Feb 20, 2017
This is known as "natural sort order", and there are at least two crates for this, though I don't know about their quality.
ranma42 commentedon Feb 20, 2017
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
T000T010T0A0T110would then not be considered sorted.clarfonthey commentedon Feb 20, 2017
@ranma42 Check out the crate I linked; I specifically track leading zeroes so that 001 comes before 01, before 1, before 10.
clarfonthey commentedon Feb 20, 2017
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.
ranma42 commentedon Feb 20, 2017
@clarcharr The issue is not with leading zeros, but with non-digit characters. For example, would
T100sort beforeT1A0? (if you interpret the numbers in hexadecimal, i.e. as 0x100 and 0x1A0, this is the obvious sorting, but if you split at every non-digit, then it is not).EDIT: oops, I changed the example. in the original one, would
T010be sorted beforeT0A0?clarfonthey commentedon Feb 21, 2017
Digits come before letters lexically, so, that'd still work as expected. Although I think that automatically coercing
a-fas numbers has the ability for too many false positives.For example,
"String1" < "StringArgument" < "String10"just doesn't make any sense.7 remaining items
Rollup merge of rust-lang#40567 - clarcharr:rustdoc-sort, r=frewsxcv
GuillaumeGomez commentedon Apr 14, 2017
Seems this issue has been fixed. Closing it.