Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sort a dictionary

Posted on 2009-04-09
6
Medium Priority
?
2,234 Views
Last Modified: 2013-11-11
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?
0
Comment
Question by:mrichmon
  • 2
  • 2
6 Comments
 
LVL 4

Expert Comment

by:Xcone
ID: 24112128
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.
0
 
LVL 4

Accepted Solution

by:
Xcone earned 2000 total points
ID: 24112216
You could also write your own ControlCodeComparer and give it as parameter to your SortedDictionary. See attached code, hopefully it'll give some ideas
    SortedDictionary<ControlCode, string> dict = new SortedDictionary<ControlCode, string>(new ControlCodeComparer<ControlCode>());
 
    public class ControlCodeComparer<T> : IComparer<T> where T:ControlCode
    {
        public int Compare(T a, T b)
        {  
            return String.Compare(((ControlCode)a).Description, ((ControlCode)b).Description);
        }
    }

Open in new window

0
 
LVL 35

Author Comment

by:mrichmon
ID: 24116601
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)
0
 
LVL 35

Author Comment

by:mrichmon
ID: 24116991
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!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question