我想创建一个排序字典的字典,其中排序的字典按降序排列键.我想尝试: private readonly IDictionarystring, SortedDictionarylong, string myDict= new Dictionarystring, SortedDictionarylong, string(); 如何设置比较器
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" } } } };