@@ -259,4 +259,50 @@ public void Deserialize_NewFullInitialQueriesDictionary_ShouldSucceed()
259259 Assert . IsType < ParameterQueryConfig > ( dict [ "0" ] ) ;
260260 Assert . IsType < ReferenceQueryConfig > ( dict [ "1" ] ) ;
261261 }
262+
263+ [ Fact ]
264+ public void OrderBy_DictionaryWithNumericStringKeys_ShouldSortNumerically ( )
265+ {
266+ var dict = new Dictionary < string , IQueryConfig >
267+ {
268+ [ "2" ] = new ParameterQueryConfig { ResourceType = "Patient" , Parameters = new List < IParameter > ( ) } ,
269+ [ "10" ] = new ReferenceQueryConfig { ResourceType = "Location" , OperationType = OperationType . Search , Paged = 100 } ,
270+ [ "1" ] = new ParameterQueryConfig { ResourceType = "Encounter" , Parameters = new List < IParameter > ( ) } ,
271+ [ "20" ] = new ReferenceQueryConfig { ResourceType = "Organization" , OperationType = OperationType . Search , Paged = 50 }
272+ } ;
273+
274+ var sorted = dict . OrderBy ( x => int . Parse ( x . Key ) ) . ToList ( ) ;
275+
276+ Assert . Equal ( "1" , sorted [ 0 ] . Key ) ;
277+ Assert . Equal ( "2" , sorted [ 1 ] . Key ) ;
278+ Assert . Equal ( "10" , sorted [ 2 ] . Key ) ;
279+ Assert . Equal ( "20" , sorted [ 3 ] . Key ) ;
280+ }
281+
282+ [ Fact ]
283+ public void OrderBy_DictionaryWithNumericStringKeys_AlphabeticSortingIsIncorrect ( )
284+ {
285+ var dict = new Dictionary < string , IQueryConfig >
286+ {
287+ [ "2" ] = new ParameterQueryConfig { ResourceType = "Patient" , Parameters = new List < IParameter > ( ) } ,
288+ [ "10" ] = new ReferenceQueryConfig { ResourceType = "Location" , OperationType = OperationType . Search , Paged = 100 } ,
289+ [ "1" ] = new ParameterQueryConfig { ResourceType = "Encounter" , Parameters = new List < IParameter > ( ) }
290+ } ;
291+
292+ // Alphabetic sort (incorrect for numeric keys)
293+ var alphabeticSort = dict . OrderBy ( x => x . Key ) . ToList ( ) ;
294+
295+ // This would give us "1", "10", "2" - wrong order
296+ Assert . Equal ( "1" , alphabeticSort [ 0 ] . Key ) ;
297+ Assert . Equal ( "10" , alphabeticSort [ 1 ] . Key ) ;
298+ Assert . Equal ( "2" , alphabeticSort [ 2 ] . Key ) ;
299+
300+ // Numeric sort (correct)
301+ var numericSort = dict . OrderBy ( x => int . Parse ( x . Key ) ) . ToList ( ) ;
302+
303+ // This gives us "1", "2", "10" - correct order
304+ Assert . Equal ( "1" , numericSort [ 0 ] . Key ) ;
305+ Assert . Equal ( "2" , numericSort [ 1 ] . Key ) ;
306+ Assert . Equal ( "10" , numericSort [ 2 ] . Key ) ;
307+ }
262308}
0 commit comments