问题  排序列表和比较器

安迪

新成员
已加入
2020年12月30日
留言内容
4
编程经验
Beginner
大家好。
谁知道,如何在此类中将比较器添加到第二排序列表:(以颜色标记)

SortedList<string, SortedList<string, 列表<arccontent>>> DB;

谢谢你。
 
由主持人最后编辑:

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,523
地点
悉尼,澳大利亚
编程经验
10+
There is no second SortedList there. That code creates a single SortedList object. It's not until you actually add an item that there are more SortedList objects and there will be one for each item you add. Each of those are distinct objects so you will have to set the comparer for each one as you create and add it. If you want it to be the same each time then the logical thing to do is to write some reusable code that does just that. That's exactly what methods are for. You could write a method that received a 细绳 key as an argument and then created a SortedList, set the comparer and then added the new item to the existing parent SortedList. Basically, a SortedList is a SortedList and you set a comparer for them all exactly the same way. There is no magic to it just because it is contained within some other data structure.
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,523
地点
悉尼,澳大利亚
编程经验
10+
and second question
否。如果您有两个问题,请创建两个线程。任何人最后需要做的就是在同一线程中进行多个不相关的对话。
 

安迪

新成员
已加入
2020年12月30日
留言内容
4
编程经验
Beginner
好的,您拆分了我的问题,但这实质上是一个问题。
如果我无法为反序列化程序准备要进一步加载的结构,
那么如何解释创建比较器呢?
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,523
地点
悉尼,澳大利亚
编程经验
10+
There's no magic to be had. If you have a SortedList<TKey,TValue> and you want to set the Comparer property then you need to create an object that implements IComparer<TKey> and assign it to the property. The alternative would be to not use that class. Instead, define your own class derived from it and create an appropriate IComparer<T> in the constructor(s). I'm not sure whether that would have any implications for the serialisation but that's why we do testing.
 
最佳 底部