Solved

Sort a dictionary

Posted on 2009-04-09
6
2,223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24111532
0
 
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 500 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

Technology Partners: 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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

734 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