@@ -7,45 +7,44 @@ namespace Microsoft.OpenApi.Extensions
7
7
/// <summary>
8
8
/// Dictionary extension methods
9
9
/// </summary>
10
- public static class DictionaryExtensions
10
+ public static class CollectionExtensions
11
11
{
12
12
/// <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.
14
14
/// </summary>
15
15
public static IDictionary < TKey , TValue > Sort < TKey , TValue > (
16
- this IDictionary < TKey , TValue > source )
16
+ this IDictionary < TKey , TValue > source ,
17
+ IComparer < TKey > comparer )
17
18
where TKey : notnull
18
19
{
19
20
#if NET7_0_OR_GREATER
20
21
ArgumentNullException . ThrowIfNull ( nameof ( source ) ) ;
22
+ ArgumentNullException . ThrowIfNull ( nameof ( comparer ) ) ;
21
23
#else
22
24
if ( source == null )
23
25
throw new ArgumentNullException ( nameof ( source ) ) ;
26
+ if ( comparer == null )
27
+ throw new ArgumentNullException ( nameof ( comparer ) ) ;
24
28
#endif
25
-
26
- return source . OrderBy ( kvp => kvp . Key )
29
+ return source . OrderBy ( kvp => kvp . Key , comparer )
27
30
. ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
28
31
}
29
32
30
33
/// <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> .
32
35
/// </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 )
37
37
{
38
38
#if NET7_0_OR_GREATER
39
- ArgumentNullException . ThrowIfNull ( nameof ( source ) ) ;
40
- ArgumentNullException . ThrowIfNull ( nameof ( comparer ) ) ;
39
+ ArgumentNullException . ThrowIfNull ( source ) ;
40
+ ArgumentNullException . ThrowIfNull ( comparer ) ;
41
41
#else
42
42
if ( source == null )
43
43
throw new ArgumentNullException ( nameof ( source ) ) ;
44
44
if ( comparer == null )
45
45
throw new ArgumentNullException ( nameof ( comparer ) ) ;
46
46
#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 ( ) ;
49
48
}
50
49
}
51
50
}
0 commit comments