Solved

Sort a dictionary

Posted on 2009-04-09
6
2,212 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 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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