C# SortedDictionary producing unusual results - Mk2
I don't think I posted enough detail in the previous question and people seemed to stop responding so I'm reposting as we need to know why this problem is happening
I'm working with a SortedDictionary and when we loop through it we get odd results.
there is a lot of nesting involved and the final dictionary is actually the child of another dictionary which is the child of another!
is t开发者_如何学Pythonhe the complete nest.
SortedDictionary<String, SortedDictionary<String, SortedDictionary<int, SortedDictionary<String, String>>>>()
The dictionary i'm looping through is
SortedDictionary<int, SortedDictionary<String, String>>
And Here is the loop:
foreach (SortedDictionary<String, String> cDic in openTrades.Values)
{
String cTimestamp = convertTimestamp(cDic["open"]);
if (!closeTrades.ContainsKey(cDic["key"]) && barArray.ContainsKey(cDic["pair"]))
{
foreach (SortedDictionary<String, String> bDic in barArray[cDic["pair"]][cDic["frame"]].Values)
{
//This is the relative Loop
}
}
}
barArray is our Primary SortedDictionary (the subject of this question) openTrades is another SortedDictionary
Now when we loop through the dictionary with an integer as an index we get varied results - IE. if we have 1,2,3,4 as the keys when looping through it may present them in this order: 4,2,1,3 which clearly doesn't make sense as this is meant to be a sorted dictionary.
Any help as quickly as possible would be greatly appreciated as im stumped on this issue. Thanks James
My gut feeling is that you are misinterpreting the results that you are seeing. In the loop that you show, you are not looping through the int key, you are looping through the list of values. Thus, you have no way to determine in which order the int keys are actually returned.
You probably have something in the SortedDictionary itself, that should indicate which int from the enclosing dictionary it belongs to. I would imagine, that there is some bug in how you are forming these values, so that the int in the key does not actually match the value.
I suggest to narrow the issue down, that you foreach through openTrade
s and not openTrades.Values
. This way you'll be able to see the real keys in the returned KeyValuePair. I'm pretty sure they will appear sorted.
精彩评论