-
Notifications
You must be signed in to change notification settings - Fork 271
feat: add sort method to dictionary #2308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Michael-Wamae
wants to merge
3
commits into
main
from
mw/provide-a-sort-method-to-reorder-dictionary
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.OpenApi.Extensions | ||
{ | ||
/// <summary> | ||
/// Dictionary extension methods. | ||
/// </summary> | ||
public static class DictionaryExtensions | ||
{ | ||
/// <summary> | ||
/// Returns a new dictionary with entries sorted by key using the default comparer. | ||
/// </summary> | ||
public static IDictionary<TKey, TValue> Sort<TKey, TValue>( | ||
this IDictionary<TKey, TValue> source) | ||
where TKey : notnull | ||
{ | ||
if (source == null) | ||
throw new ArgumentNullException(nameof(source)); | ||
|
||
return source | ||
.OrderBy(kvp => kvp.Key) | ||
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a new dictionary with entries sorted by key using a custom comparer. | ||
/// </summary> | ||
public static IDictionary<TKey, TValue> Sort<TKey, TValue>( | ||
this IDictionary<TKey, TValue> source, | ||
IComparer<TKey> comparer) | ||
where TKey : notnull | ||
{ | ||
if (source == null) | ||
throw new ArgumentNullException(nameof(source)); | ||
if (comparer == null) | ||
throw new ArgumentNullException(nameof(comparer)); | ||
|
||
return source | ||
.OrderBy(kvp => kvp.Key, comparer) | ||
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); | ||
} | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
test/Microsoft.OpenApi.Tests/Extensions/DictionaryExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.OpenApi.Extensions; | ||
using Xunit; | ||
|
||
namespace Microsoft.OpenApi.Tests.Extensions | ||
{ | ||
public class DictionaryExtensionsTests | ||
{ | ||
[Fact] | ||
public void ShouldSortStringIntDictionaryInAscendingOrder() | ||
{ | ||
var dict = new Dictionary<string, int> { { "b", 2 }, { "a", 1 } }; | ||
var result = dict.Sort(); | ||
Assert.Equal(["a", "b"], result.Keys); | ||
} | ||
|
||
[Fact] | ||
public void ShouldReturnEmptyDictionaryWhenSourceIsEmpty() | ||
{ | ||
var dict = new Dictionary<string, int>(); | ||
var result = dict.Sort(); | ||
Assert.Empty(result); | ||
} | ||
|
||
[Fact] | ||
public void ShouldKeepOrderWhenDictionaryIsAlreadySorted() | ||
{ | ||
var dict = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } }; | ||
var result = dict.Sort(); | ||
Assert.Equal(["a", "b"], result.Keys); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSortNumericKeysNaturally() | ||
{ | ||
var dict = new Dictionary<int, string> { { 10, "a" }, { 1, "b" } }; | ||
var result = dict.Sort(); | ||
Assert.Equal([1, 10], result.Keys); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSortDateTimeKeysInAscendingOrder() | ||
{ | ||
var now = DateTime.Now; | ||
var later = now.AddHours(1); | ||
|
||
var dict = new Dictionary<DateTime, string> | ||
{ | ||
[later] = "future", | ||
[now] = "present" | ||
}; | ||
|
||
var result = dict.Sort(); | ||
Assert.Equal([now, later], result.Keys); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSortWithCustomDescendingComparer() | ||
{ | ||
var dict = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } }; | ||
var result = dict.Sort(Comparer<string>.Create((x, y) => y.CompareTo(x))); | ||
Assert.Equal(["b", "a"], result.Keys); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSortDictionaryWithComplexValueTypes() | ||
{ | ||
var dict = new Dictionary<string, ISet<string>> | ||
{ | ||
{ "z", new HashSet<string> { "value1" } }, | ||
{ "a", new HashSet<string> { "value2" } } | ||
}; | ||
|
||
var result = dict.Sort(); | ||
Assert.Equal(["a", "z"], result.Keys); | ||
Assert.Equal(new HashSet<string> { "value2" }, result["a"]); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSortDictionaryWithNullValues() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "b", null }, | ||
{ "a", "value" } | ||
}; | ||
|
||
var result = dict.Sort(); | ||
Assert.Equal(["a", "b"], result.Keys); | ||
Assert.Null(result["b"]); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSortDictionaryOfDictionariesByOuterKey() | ||
{ | ||
var dict = new Dictionary<string, Dictionary<string, string>> | ||
{ | ||
["z"] = new Dictionary<string, string> { { "x", "1" } }, | ||
["a"] = new Dictionary<string, string> { { "y", "2" } } | ||
}; | ||
|
||
var result = dict.Sort(); | ||
Assert.Equal(["a", "z"], result.Keys); | ||
Assert.Equal("2", result["a"]["y"]); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.