mrichmon
asked on
Sort a dictionary
I have a hashed structure that is bound to a repeater, that I would like to sort on display:
Dictionary<Level, Dictionary<ControlCode, Dictionary<Person, List<Roles>>>>
It is bound into a repeater with nested repeaters - and displays appropriately, however unsorted. I would like it to be sorted by level, then by the Description property of the ControlCode, then by the LastName of the Person.
I am having trouble with sorting the ControlCode level.
The ControlCode class does not implement iComparable - and even if it did it would most likely sort by the code - where in this case I need to sort by the Description. (Otherwise I could change to SortedDictionary). I thought that I should be able to use LINQ to sort this (perhaps the OrderBy), but cannot seem to figure it out.
Based on the way the data gets into the structure - I cannot sort going in - it needs to be sorted once the structure is complete.
Any help?
Dictionary<Level, Dictionary<ControlCode, Dictionary<Person, List<Roles>>>>
It is bound into a repeater with nested repeaters - and displays appropriately, however unsorted. I would like it to be sorted by level, then by the Description property of the ControlCode, then by the LastName of the Person.
I am having trouble with sorting the ControlCode level.
The ControlCode class does not implement iComparable - and even if it did it would most likely sort by the code - where in this case I need to sort by the Description. (Otherwise I could change to SortedDictionary). I thought that I should be able to use LINQ to sort this (perhaps the OrderBy), but cannot seem to figure it out.
Based on the way the data gets into the structure - I cannot sort going in - it needs to be sorted once the structure is complete.
Any help?
take a look at http://stackoverflow.com/questions/451717/how-do-i-convert-from-a-dictionary-to-a-sorteddictionary-using-linq-in-c
Can't you create a new class which inherits from ControlCode and implement IComparable for it. That way you can fully customize how they're compared which allows you to simply use sorted dictionary.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Xcone,
>>Can't you create a new class which inherits from ControlCode and implement IComparable for it
In this case no, however, I like the second idea - I did not realize that there was a constructor for a SortedDictionary that took in a custom comparer as a parameter. That works great! I guess I should have looked at the SortedDictionary documentation ;o)
>>Can't you create a new class which inherits from ControlCode and implement IComparable for it
In this case no, however, I like the second idea - I did not realize that there was a constructor for a SortedDictionary that took in a custom comparer as a parameter. That works great! I guess I should have looked at the SortedDictionary documentation ;o)
ASKER
Sorry - wrong comment got marked as answer, - I have asked it be corrected so Xcone you will get the points/accepted solution.
Thanks again - actually that gave me a way to do something much fancier than I originally thought I could!
Thanks again - actually that gave me a way to do something much fancier than I originally thought I could!