fmt-rfcs icon indicating copy to clipboard operation
fmt-rfcs copied to clipboard

Consider using human/natural sort order

Open tanriol opened this issue 6 years ago • 7 comments

Names used in libraries quite often contain numbers. In general, a sequence of digits in a name seems to be usually a number, not just a sequence of digits. However, rustfmt's sort order does not take that into account. For example, if I import several nom parsers for various integers, rustfmt suggests the following order

use nom::number::complete::{le_u128, le_u16, le_u32, le_u64, le_u8};

which feels really weird compared to

use nom::number::complete::{le_u8, le_u16, le_u32, le_u64, le_u128};

It would be great if the rustfmt sort order matched the natural expectations.

tanriol avatar Aug 27 '19 11:08 tanriol

Agreed. We should use the versionsort/strverscmp algorithm, documented at https://www.gnu.org/software/libc/manual/html_node/String_002fArray-Comparison.html .

joshtriplett avatar Aug 28 '19 07:08 joshtriplett

Hm. Looking at the linked documentation

Digits are determined by the isdigit function and are thus subject to the current locale.

is my expectation correct that rustfmt should not rely on the environment locale and should sort according to some default?

Also, what's the process here? Is anything needed before implementation and rustfmt PR?

tanriol avatar Aug 28 '19 13:08 tanriol

@tanriol it's likely that as an MVP, we should just do strverscmp in the C locale (i.e., 0-9 are digits and are read from left to right, big end to small end)

strega-nil avatar Aug 28 '19 16:08 strega-nil

Would it be better to depend on some crate providing natural/version sort or to write an internal function for that?

tanriol avatar Aug 28 '19 22:08 tanriol

@tanriol I meant that as a general description of the algorithm. I would expect us to use 0-9 here for now, and I think we can implement the algorithm ourselves in Rust unless there's a crate handy that you already know of.

joshtriplett avatar Aug 28 '19 23:08 joshtriplett

Working on a PR and testing it, I'm seeing contradictions between actual tests and comment in them. Which ones should I follow?

tanriol avatar Aug 29 '19 19:08 tanriol

The PR is up as rust-lang/rustfmt#3764.

tanriol avatar Aug 29 '19 21:08 tanriol