Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sort a dictionary

Posted on 2009-04-09
6
Medium Priority
?
2,230 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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!
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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