如何使用 Comparer 初始化嵌套的排序字典?

How to initialize a nested Sorted Dictionary with Comparer?

我想创建一个排序字典的字典,排序字典的键按降序排列。我正在尝试:

private readonly IDictionary<string, SortedDictionary<long, string>> myDict= new Dictionary<string, SortedDictionary<long, string>>();

如何设置比较器:

Comparer<long>.Create((x, y) => y.CompareTo(x))

对于嵌套字典?

使用此代码:

var myDict = new Dictionary<string, SortedDictionary<long, string>>();

您正在初始化一个空字典,其中不包含任何嵌套字典。做后者:

var comparer = Comparer<long>.Create((x, y) => y.CompareTo(x));

var myDict = new Dictionary<string, SortedDictionary<long, string>
{
    // Add a SortedDictionary to myDict
    { "dict1", new SortedDictionary<long, string>>(comparer) 
        {
            // Add a key-value pair to the SortedDictionary
            { 123, "nestedValue" }
        }
    }
};