In C# how do I order elements inside a DictionaryBase by their ID

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Randel Ramirez
    New Member
    • May 2011
    • 1

    In C# how do I order elements inside a DictionaryBase by their ID

    Here is the code I am using
    Code:
      foreach (var item in peeps)
                {
                    Console.WriteLine(((Person)item).name);
                    
                }
    I know that I can I replace the var item with something like:
    Code:
    foreach (Person p in peeps)
                {
                    Console.WriteLine(p.name);
                    
                }
    I am using class called People that derives from DictionaryBase while the Person class is the object I'm trying to order by/sort according to the ID that I used for the DictionaryBase.
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    #2
    Im not that good, so this could be a redundant way of doing it. Read the values and place them into a new array list. Then sort the array list. There should be an easier way since you are already reading an array. Maybe the comparable class has something.


    Comment

    • Joseph Martell
      Recognized Expert New Member
      • Jan 2010
      • 198

      #3
      There are a couple ways of doing this. The best one depends on your specific situation and what version of the .Net framework you are using.

      If you are using .Net 3.5 or greater, then I would highly suggest that you switch to a generic SortedDictionar y class. It does all of the leg work for you and should provide all of the functionality you are after and more. Check out the link above to the MSDN documentation. It is quite in depth, including a comparison to the SortedList class which may or may not also be suitable for your application.

      A basic declaration of your "Peeps" member from your example would look like this:

      Code:
      //using System.Collections.Generic; SortedDictionary<int, Person> Peeps = new SortedDictionary<int, Person>();
      To use the Peeps dictionary all you would do is something like this:
      Code:
      Person myPerson = new Person(0);
      Peeps.Add(myPerson.Id, myPerson);

      If you are using a version of .Net that pre-dates 3.5, then generics are not available to you and you are left deriving your "People" class from the DictionaryBase yourself. In that case, you would have to override the "Add" and "Value" members (as well as any other property or method that adds elements to the collection) to make sure that you are maintaining a sorted dictionary. This would probably be a fairly costly procedure though. You would basically have to find where your added item would go in the sorted dictionary, remove all of the elements in the dictionary after that location, add the new item, and then add all of the other items back into the dictionary.

      Generics are by far the easier solution in this case, but you are limited by framework version. Hope that helps.

      Comment

      Working...