Question Sortedlist and comparer

AndyC

New member
Joined
Dec 30, 2020
Messages
4
Programming Experience
Beginner
Hello, guys.
Anybody knows, how to add comparer to 2-nd sortedlist in this class: (marked by color)

SortedList<string, SortedList<string, List<arccontent>>> DB;

Thank you.
 
Last edited by a moderator:
and second question
No. If you have two questions then create two threads. The last thing anyone needs is multiple, unrelated conversations in the same thread.
 
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 string 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.
 
OK, you split my question, but It was essentially one question.
If I cannot pre-prepare the structure for further loading by deserializer,
so how to explain to it to create comparer?
 
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.
 
Back
Top Bottom