Solved

Sort a dictionary

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now