C# SortedDictionary 产生异常结果
我正在使用 SortedDictionary,其中键是整数,值是字符串。
SortedDictionary<int,string> dic = new SortedDictionary<int,string>();
现在假设我添加值
dic.Add(100,"String 1");
dic.Add(1113,"String 2");
dic.Add(1,"String 3");
dic.Add(70,"String 4");
,然后执行 foreach 循环,
foreach(string item in dic.Values) {
Console.WriteLine(item);
}
然后这些值永远不会以正确的顺序出现,它们以几乎随机的顺序出现,这与普通字典的行为类似。有人知道为什么吗?我错过了/做错了什么吗?
PS:当我说它以随机顺序出现时,我的意思是关键顺序而不是值,所以它的结果像 1113,70,1,100
似乎我可能过度简化了问题,但它不应该有什么区别,有一个涉及很多嵌套,最终的字典实际上是另一个字典的子字典,而另一个字典又是另一个字典的子字典!
SortedDictionary<String, SortedDictionary<String, SortedDictionary<int, SortedDictionary<String, String>>>>()
我正在循环的字典是
SortedDictionary<int, SortedDictionary<String, String>>
这里是所要求的循环:
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是我们的Primary SortedDictionary(这个问题的主题) openTrades 是另一个 SortedDictionary
谢谢 詹姆斯
I'm working with a SortedDictionary where the key is integer and value is string.
SortedDictionary<int,string> dic = new SortedDictionary<int,string>();
Now say I add values like
dic.Add(100,"String 1");
dic.Add(1113,"String 2");
dic.Add(1,"String 3");
dic.Add(70,"String 4");
and then do a foreach loop like
foreach(string item in dic.Values) {
Console.WriteLine(item);
}
then the values never come out in the correct order, they come out in an almost random order which is similar behaviour to a normal Dictionary. Anyone got any ideas why? am I missing / doing something wrong?
PS: When I say it's coming out in a random order I mean key order not value so it's coming out like 1113,70,1,100
It seems I may have over simplified the problem, but it shouldn't make a difference, there is a lot of nesting involved and the final dictionary is actually the child of another dictionary which is the child of another!
SortedDictionary<String, SortedDictionary<String, SortedDictionary<int, SortedDictionary<String, String>>>>()
The dictionary i'm looping through is
SortedDictionary<int, SortedDictionary<String, String>>
Here is the loop as requested:
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
Thanks
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SortedDictionary
按 键排序 不是价值。如果执行以下操作,
您将看到它按键排序的顺序打印出来:
SortedDictionary
sorts on the key not the value.If you do the following
You will see that it prints out in sorted order by key:
我不确定你认为输出应该是什么,但对我来说这是
正确的。也许您的印象是
SortedDictionary
维护插入顺序?快速浏览一下文档会告诉您事实并非如此。这些值通过键上的Comparer
进行排序(假设未提供自定义比较器。)I'm not sure what you think the output should be, but for me it is
Which is correct. Perhaps you are under the impression that a
SortedDictionary
maintains insertion order? A quick review of the documentation will tell you that is not the case. The values are sorted via aComparer<T>
on the key (assuming no custom Comparer is provided.)和其他人一样,我的第一印象是,如果字典是按键排序而不是按值排序,您会感到困惑。但读了你的评论,这似乎不是问题。
所以我只是(也像其他人一样)将代码粘贴到 Visual Studio 中并让它运行。输出是(如预期的):
如果您的机器上的顺序不同,那么那里似乎发生了一些非常奇怪的事情。也许您可以再次运行,但对 foreach 语句进行一些更改:
现在在 item 中您将得到一个
KeyValuePair
,它将很好地打印出来,如下所示:所以您'将再次检查您获得的每个值的确切键属于什么,也许会找到另一个线索为什么您会得到您不期望的排序。
As all the others my first impression was you are confused if the dictionary is sorted by keys and not by values. But reading your comments this doesn't seem the problem.
So i simply (like the others too) pasted the code into Visual Studio and let it run. The output was (as expected):
If the order differs on your machine, there seems to be something really weird going on there. Maybe you can make another run, but make a little change to your foreach statement:
Now in item you'll get a
KeyValuePair<int, string>
, which will nicely printed out like this:So you'll get another check what exact key belongs to each value you got and maybe find another clue why you get a sorting you don't expect.
问题出在你的 foreach 循环上。 SortedDictionary 类公开值列表,但该列表未排序,因此您只是从未排序的列表中读取。再次阅读代码,我意识到该列表是按排序顺序呈现的。我认为发生的事情是您感到困惑,因为您以某种方式期望它对“值”字段进行排序。 SortedDictionary 对 Key 字段(add 语句中的整数)进行排序。
The problem lies in your foreach loop. The SortedDictionary class exposes the list of values, but that list isn't sorted, so you are just reading from a unsorted list.Reading the code again, I realized that the list is being presented in a sorted order. What I think happened is that you got confused because you somehow expected it to sort through the Value field. SortedDictionary sorts through the Key field (the integers in your add statement).