No sooner did I write a blog posting on how to Sort a Dictionary than I found this suggesting that you cannot Sort a Dictionary. I disagree however. The means mentioned earlier works. One may move "half" of a dictionary out to a list to inspect the order within it.
foreach (KeyValuePair<Guid,string> pair in foo)
{
myList.Add(pair.Value);
}
Here is the same thing in the shape of a Lambda utilizing .AddRange:
myList.AddRange(foo.Select(pair => pair.Value));
Verify the sorting with testing by pulling the "half" of the Dictionary you want out to a list and looking at the order of the list:
Assert.AreEqual(3, foo.Count);
Assert.AreEqual("a", myList[0]);
Assert.AreEqual("b", myList[1]);
Assert.AreEqual("c", myList[2]);
No comments:
Post a Comment