SortedDictionary is supposed to always be sorted by its key.
Now consider the following code:
public class ContentKey
{
public int key_num = 0;
}
public static void Main(string[] args)
{
ContentKey a = new ContentKey() {key_num = 10};
ContentKey b = new ContentKey() {key_num = 20};
ContentKey c = new ContentKey() {key_num = 30};
SortedDictionary<ContentKey, string> dict = new SortedDictionary<ContentKey, string>(new KeyComparer());
dict.Add(a, "A");
dict.Add(c, "C");
dict.Add(b, "B");
foreach(var key_value in dict)
{
Console.WriteLine("Key val: " + key_value.Key.key_num + " Str: " + key_value.Value);
}
a.key_num = 50;
foreach(var key_value in dict)
{
Console.WriteLine("Key val: " + key_value.Key.key_num + " Str: " + key_value.Value);
}
}
public class KeyComparer : IComparer<ContentKey>
{
public int Compare(ContentKey a, ContentKey b)
{
return a.key_num.CompareTo(b.key_num);
}
}
https://dotnetfiddle.net/8wwK8P
I would expect it to print: A, B, C for the first batch and then B, C, A. However it's A,B,C in both cases. This means the dictionary has not "realized" that the state of the key has in fact changed.
How can this be solved (without removing and reinserting A, since in practice I may not know which of many keys has changed)?
In my opinion you're going about things wrong here. A list or an array would be more appropriate but here's the solution to your problem. You can remove the stuff related to the Boolean value if you'd like, that's only there for your benefit of tracking the values.
What this code does is it reads in the dictionary by reference and adds them to a sorted dictionary same as you did earlier in your code, it then overrides the dictionary object with the new values sorted.