Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions src/Microsoft.OpenApi/Extensions/DictionaryExtensions.cs
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 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.IDictionary<TKey, TValue> Sort<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> source)
where TKey : notnull { }
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)
where TKey : notnull { }
}
public static class EnumExtensions
{
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2075", Justification="Fields are never trimmed for enum types.")]
Expand Down