473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Randel Ramirez
Here is the code I am using
Expand|Select|Wrap|Line Numbers
  1.   foreach (var item in peeps)
  2.             {
  3.                 Console.WriteLine(((Person)item).name);
  4.  
  5.             }
  6.  
I know that I can I replace the var item with something like:
Expand|Select|Wrap|Line Numbers
  1. foreach (Person p in peeps)
  2.             {
  3.                 Console.WriteLine(p.name);
  4.  
  5.             }
  6.  
  7.  
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.
May 29 '11 #1
2 2116
Brian Connelly
103 100+
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.


http://msdn.microsoft.com/en-us/libr...ase.aspx#Y2394
May 30 '11 #2
Joseph Martell
198 Expert 128KB
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 SortedDictionary 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:

Expand|Select|Wrap|Line Numbers
  1. //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:
Expand|Select|Wrap|Line Numbers
  1. Person myPerson = new Person(0);
  2. Peeps.Add(myPerson.Id, myPerson);
  3.  

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.
Jun 14 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: google | last post by:
I would like to print a table, using a while loop to insert as many blank text fields as players specified by the user. How do I put html inside a php loop. This is what I have.... <body>...
9
by: Troy Parker | last post by:
Hello, When the example HTML below is viewed on IE 6, "Text" and "Heading" line up. On Mozilla 1.5, they don't; there's blank space above "Heading". What is the best way to make Mozilla look...
8
by: Jukka K. Korpela | last post by:
I just noticed that most browsers render <table border="1"><tr><td><p>foo</p></td></tr></table> the same way as <table border="1"><tr><td>foo</td></tr></table> That is, they ignore the p...
2
by: Falc2199 | last post by:
i all, I have an HTML table within which I have a span. When the user clicks a button I want to update the contents of this span with form elements table elements (TR,TD). The problem is that...
1
by: Kenneth | last post by:
This is probably very simple question but how do you access elements inside the <div> tag? I need to access the value of a textfield inside a <div> tag. Thanks.
3
by: lawrence | last post by:
This is a follow up question to the conversation that started here: http://groups.google.com/groups?hl=en&lr=&safe=off&selm=da7e68e8.0410010901.18a813c9%40posting.google.com I tried nesting...
1
by: Max Evans | last post by:
I have a XML file, which contains itemid-elements, e.g.: <itemid>3</itemid> <itemid>12</itemid> Now I want to convert these IDs to the corresponding name via XSLT. I thought I could do it this...
2
by: Juliano.net | last post by:
How can I get a list of elements inside a tag and when I find one element of a type that I choose I can change its ID and CLASS attribute? Ex: <div id="content"> <!-- I want to get the list of...
9
by: Daniel Kabs | last post by:
Hello there again! On my quest to remove layout tables in favor of CSS controlled elements, I am faced with quite a problem. A table cell that has no explicit width attribute set usually...
1
by: Vivek | last post by:
Hi All, I need some help with showing option elements of a multiple select list in a particular order. Is it possible to order the elements according to the value of the option or the text of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.