@@ -12,34 +12,27 @@ public static class DictionaryExtensions
1212 /// <summary>
1313 /// Returns a new dictionary with entries sorted by key using the default comparer.
1414 /// </summary>
15- public static IDictionary < TKey , TValue > Sort < TKey , TValue > (
15+ public static SortedDictionary < TKey , TValue > Sort < TKey , TValue > (
1616 this IDictionary < TKey , TValue > source )
1717 where TKey : notnull
1818 {
19- if ( source == null )
20- throw new ArgumentNullException ( nameof ( source ) ) ;
19+ Utils . CheckArgumentNull ( source ) ;
2120
22- return source
23- . OrderBy ( kvp => kvp . Key )
24- . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
21+ return new SortedDictionary < TKey , TValue > ( source ) ;
2522 }
2623
2724 /// <summary>
2825 /// Returns a new dictionary with entries sorted by key using a custom comparer.
2926 /// </summary>
30- public static IDictionary < TKey , TValue > Sort < TKey , TValue > (
27+ public static SortedDictionary < TKey , TValue > Sort < TKey , TValue > (
3128 this IDictionary < TKey , TValue > source ,
3229 IComparer < TKey > comparer )
3330 where TKey : notnull
3431 {
35- if ( source == null )
36- throw new ArgumentNullException ( nameof ( source ) ) ;
37- if ( comparer == null )
38- throw new ArgumentNullException ( nameof ( comparer ) ) ;
32+ Utils . CheckArgumentNull ( source ) ;
33+ Utils . CheckArgumentNull ( comparer ) ;
3934
40- return source
41- . OrderBy ( kvp => kvp . Key , comparer )
42- . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
35+ return new SortedDictionary < TKey , TValue > ( source , comparer ) ;
4336 }
4437 }
4538}
0 commit comments