@@ -112,6 +112,90 @@ fn bar() {}
112112fn baz () {}
113113```
114114
115+ ### Sorting
116+
117+ In various cases, the default Rust style specifies to sort things. If not
118+ otherwise specified, such sorting should be "version sorting", which ensures
119+ that (for instance) ` x8 ` comes before ` x16 ` even though the character ` 1 ` comes
120+ before the character ` 8 ` . (If not otherwise specified, version-sorting is
121+ lexicographical.)
122+
123+ For the purposes of the Rust style, to compare two strings for version-sorting:
124+
125+ - Process both strings from beginning to end as two sequences of maximal-length
126+ chunks, where each chunk consists either of a sequence of characters other
127+ than ASCII digits, or a sequence of ASCII digits (a numeric chunk), and
128+ compare corresponding chunks from the strings.
129+ - To compare two numeric chunks, compare them by numeric value, ignoring
130+ leading zeroes. If the two chunks have equal numeric value, but different
131+ numbers of leading digits, and this is the first time this has happened for
132+ these strings, treat the chunks as equal (moving on to the next chunk) but
133+ remember which string had more leading zeroes.
134+ - To compare two chunks if both are not numeric, compare them by Unicode
135+ character lexicographically, except that ` _ ` (underscore) sorts immediately
136+ after ` ` (space) but before any other character. (This treats underscore as
137+ a word separator, as commonly used in identifiers.)
138+ - If the use of version sorting specifies further modifiers, such as sorting
139+ non-lowercase before lowercase, apply those modifiers to the lexicographic
140+ sort in this step.
141+ - If the comparison reaches the end of the string and considers each pair of
142+ chunks equal:
143+ - If one of the numeric comparisons noted the earliest point at which one
144+ string had more leading zeroes than the other, sort the string with more
145+ leading zeroes first.
146+ - Otherwise, the strings are equal.
147+
148+ Note that there exist various algorithms called "version sorting", which
149+ generally try to solve the same problem, but which differ in various ways (such
150+ as in their handling of numbers with leading zeroes). This algorithm
151+ does not purport to precisely match the behavior of any particular other
152+ algorithm, only to produce a simple and satisfying result for Rust formatting.
153+ In particular, this algorithm aims to produce a satisfying result for a set of
154+ symbols that have the same number of leading zeroes, and an acceptable and
155+ easily understandable result for a set of symbols that has varying numbers of
156+ leading zeroes.
157+
158+ As an example, version-sorting will sort the following strings in the order
159+ given:
160+ - ` _ZYWX `
161+ - ` u_zzz `
162+ - ` u8 `
163+ - ` u16 `
164+ - ` u32 `
165+ - ` u64 `
166+ - ` u128 `
167+ - ` u256 `
168+ - ` ua `
169+ - ` usize `
170+ - ` uz `
171+ - ` v000 `
172+ - ` v00 `
173+ - ` v0 `
174+ - ` v0s `
175+ - ` v00t `
176+ - ` v0u `
177+ - ` v001 `
178+ - ` v01 `
179+ - ` v1 `
180+ - ` v009 `
181+ - ` v09 `
182+ - ` v9 `
183+ - ` v010 `
184+ - ` v10 `
185+ - ` w005s09t `
186+ - ` w5s009t `
187+ - ` x64 `
188+ - ` x86 `
189+ - ` x86_32 `
190+ - ` x86_64 `
191+ - ` x86_128 `
192+ - ` x87 `
193+ - ` Z_YWX `
194+ - ` ZY_WX `
195+ - ` ZYW_X `
196+ - ` ZYWX `
197+ - ` ZYWX_ `
198+
115199### [ Module-level items] ( items.md )
116200
117201### [ Statements] ( statements.md )
0 commit comments