@@ -21,6 +21,12 @@ it.each([
2121 [ '2' , '1' , GREATER ] ,
2222 [ '1' , '10' , LESS ] ,
2323 [ '10' , '1' , GREATER ] ,
24+
25+ // Numbers of different lengths
26+ [ '75' , '700' , LESS ] ,
27+ [ '700' , '75' , GREATER ] ,
28+ [ '75' , '770' , LESS ] ,
29+ [ '770' , '75' , GREATER ] ,
2430] ) ( 'should compare "%s" with "%s" as "%d"' , ( a , b , expected ) => {
2531 expect ( Math . sign ( compare ( a , b ) ) ) . toBe ( expected )
2632} )
@@ -124,3 +130,39 @@ it('should sort strings with multiple numbers consistently using the `compare` f
124130 ]
125131 ` )
126132} )
133+
134+ it ( 'sort is stable' , ( ) => {
135+ // Heap's algorithm for permutations
136+ function * permutations < T > ( input : T [ ] ) {
137+ let pos = 1
138+ let stack = input . map ( ( ) => 0 )
139+
140+ yield input . slice ( )
141+
142+ while ( pos < input . length ) {
143+ if ( stack [ pos ] < pos ) {
144+ let k = pos % 2 == 0 ? 0 : stack [ pos ]
145+ ; [ input [ k ] , input [ pos ] ] = [ input [ pos ] , input [ k ] ]
146+ yield input . slice ( )
147+ ++ stack [ pos ]
148+ pos = 1
149+ } else {
150+ stack [ pos ] = 0
151+ ++ pos
152+ }
153+ }
154+ }
155+
156+ let classes = [ 'duration-initial' , 'duration-75' , 'duration-150' , 'duration-700' , 'duration-1000' ]
157+
158+ for ( let permutation of permutations ( classes ) ) {
159+ let sorted = [ ...permutation ] . sort ( compare )
160+ expect ( sorted ) . toEqual ( [
161+ 'duration-75' ,
162+ 'duration-150' ,
163+ 'duration-700' ,
164+ 'duration-1000' ,
165+ 'duration-initial' ,
166+ ] )
167+ }
168+ } )
0 commit comments