I understand that SortedDictionary is not available in WP7…so have to sort it yourself.
I have found a number of examples and am currently using this code
Dictionary<string, TimetableClass> AllClasses = new Dictionary<string, TimetableClass>()
{
{ "Maths", new TimetableClass {ClassName="Maths", Location="RM1"}},
{ "Physics", new TimetableClass {ClassName="Physics", Location="PM1"}},
{ "English", new TimetableClass {ClassName="English", Location="PM1"}},
{ "Algebra", new TimetableClass {ClassName="Algebra", Location="A1"}}
};
var sortedDict = new Dictionary<string, TimetableClass>();
foreach (KeyValuePair<string, TimetableClass> singleclass in AllClasses.OrderBy(key => key.Value))
{
sortedDict.Add(singleclass.Key, singleclass.Value);
}
But when I run this I get an exception on the foreach?
Exception is an Unhandled Argument Exception – “Value does bot fall within the expected range”
Really have no idea what I have done wrong here.
Any help appreciated.
- Thanks
This should work
Alternately, if you want to sort based on Object(Value), try this
And finally, you can also avail this instead of a foreach loop and without the need of an extra ‘sortedDict’ dictionary.