Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Microsoft.OpenApi/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 SortedDictionary<TKey, TValue> Sort<TKey, TValue>(
this Dictionary<TKey, TValue> source)
where TKey : notnull
{
Utils.CheckArgumentNull(source);

return new SortedDictionary<TKey, TValue>(source);
}

/// <summary>
/// Returns a new dictionary with entries sorted by key using a custom comparer.
/// </summary>
public static SortedDictionary<TKey, TValue> Sort<TKey, TValue>(
this Dictionary<TKey, TValue> source,
IComparer<TKey> comparer)
where TKey : notnull
{
Utils.CheckArgumentNull(source);
Utils.CheckArgumentNull(comparer);

return new SortedDictionary<TKey, TValue>(source, comparer);
}
}
}
108 changes: 108 additions & 0 deletions test/Microsoft.OpenApi.Tests/Extensions/DictionaryExtensionsTests.cs
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"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ namespace Microsoft.OpenApi.Expressions
}
namespace Microsoft.OpenApi.Extensions
{
public static class DictionaryExtensions
{
public static System.Collections.Generic.SortedDictionary<TKey, TValue> Sort<TKey, TValue>(this System.Collections.Generic.Dictionary<TKey, TValue> source)
where TKey : notnull { }
public static System.Collections.Generic.SortedDictionary<TKey, TValue> Sort<TKey, TValue>(this System.Collections.Generic.Dictionary<TKey, TValue> source, System.Collections.Generic.IComparer<TKey> comparer)
where TKey : notnull { }
}
public static class EnumExtensions
{
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2075", Justification="Fields are never trimmed for enum types.")]
Expand Down