@@ -7,45 +7,44 @@ namespace Microsoft.OpenApi.Extensions
77 /// <summary>
88 /// Dictionary extension methods
99 /// </summary>
10- public static class DictionaryExtensions
10+ public static class CollectionExtensions
1111 {
1212 /// <summary>
13- /// Returns a new dictionary with entries sorted by key using the default comparer.
13+ /// Returns a new dictionary with entries sorted by key using a custom comparer.
1414 /// </summary>
1515 public static IDictionary < TKey , TValue > Sort < TKey , TValue > (
16- this IDictionary < TKey , TValue > source )
16+ this IDictionary < TKey , TValue > source ,
17+ IComparer < TKey > comparer )
1718 where TKey : notnull
1819 {
1920#if NET7_0_OR_GREATER
2021 ArgumentNullException . ThrowIfNull ( nameof ( source ) ) ;
22+ ArgumentNullException . ThrowIfNull ( nameof ( comparer ) ) ;
2123#else
2224 if ( source == null )
2325 throw new ArgumentNullException ( nameof ( source ) ) ;
26+ if ( comparer == null )
27+ throw new ArgumentNullException ( nameof ( comparer ) ) ;
2428#endif
25-
26- return source . OrderBy ( kvp => kvp . Key )
29+ return source . OrderBy ( kvp => kvp . Key , comparer )
2730 . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
2831 }
2932
3033 /// <summary>
31- /// Returns a new dictionary with entries sorted by key using a custom comparer .
34+ /// Sorts any IEnumerable<T> using the specified comparer and returns a List</T> .
3235 /// </summary>
33- public static IDictionary < TKey , TValue > Sort < TKey , TValue > (
34- this IDictionary < TKey , TValue > source ,
35- IComparer < TKey > comparer )
36- where TKey : notnull
36+ public static List < T > Sort < T > ( this IEnumerable < T > source , IComparer < T > comparer )
3737 {
3838#if NET7_0_OR_GREATER
39- ArgumentNullException . ThrowIfNull ( nameof ( source ) ) ;
40- ArgumentNullException . ThrowIfNull ( nameof ( comparer ) ) ;
39+ ArgumentNullException . ThrowIfNull ( source ) ;
40+ ArgumentNullException . ThrowIfNull ( comparer ) ;
4141#else
4242 if ( source == null )
4343 throw new ArgumentNullException ( nameof ( source ) ) ;
4444 if ( comparer == null )
4545 throw new ArgumentNullException ( nameof ( comparer ) ) ;
4646#endif
47- return source . OrderBy ( kvp => kvp . Key , comparer )
48- . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
47+ return source . OrderBy ( item => item , comparer ) . ToList ( ) ;
4948 }
5049 }
5150}
0 commit comments