Skip to content

Commit 37e130d

Browse files
committed
update dictionary extension
1 parent 8657b87 commit 37e130d

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

src/Microsoft.OpenApi/Extensions/DictionaryExtensions.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ namespace Microsoft.OpenApi.Extensions
144144
{
145145
public static class DictionaryExtensions
146146
{
147-
public static System.Collections.Generic.IDictionary<TKey, TValue> Sort<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> source)
147+
public static System.Collections.Generic.SortedDictionary<TKey, TValue> Sort<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> source)
148148
where TKey : notnull { }
149-
public static System.Collections.Generic.IDictionary<TKey, TValue> Sort<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> source, System.Collections.Generic.IComparer<TKey> comparer)
149+
public static System.Collections.Generic.SortedDictionary<TKey, TValue> Sort<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> source, System.Collections.Generic.IComparer<TKey> comparer)
150150
where TKey : notnull { }
151151
}
152152
public static class EnumExtensions

0 commit comments

Comments
 (0)