473,396 Members | 1,706 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,396 software developers and data experts.

sort order when iterating a hashtable

Hi,
Anyone know if its possible to iterate through a hashtable in the same order
that the table was populated?
Im currently using an enumerator to iterate, but the order is quit different
from when the items where inserted into the hashtable.

Thanks
D.
Dec 13 '06 #1
4 4746
This is expected. Hash-tables do not guarantee to respect insertion
order. If you need this ability, consider maintaining a separate flat
list / array for this purpose.

Marc
Dec 13 '06 #2
Hi,

You may also want to consider SortedList<T, Kif your results are sorted to
begin with.

--
Dave Sexton

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eg**************@TK2MSFTNGP02.phx.gbl...
This is expected. Hash-tables do not guarantee to respect insertion order.
If you need this ability, consider maintaining a separate flat list /
array for this purpose.

Marc

Dec 13 '06 #3
Dahab,

That's not possible because of the way hash tables work. What you want
is a dictionary that preserves temporal ordering similar to how the VB
Collection object works. There's no suitable collection in the BCL
(that I know), but you can find one in the Microsoft.VisualBasic.dll
assembly. Also, there is an implementation at the following link.
It's super easy to implement.

http://www.codeproject.com/csharp/KeyedList.asp

Brian

Dahab wrote:
Hi,
Anyone know if its possible to iterate through a hashtable in the same order
that the table was populated?
Im currently using an enumerator to iterate, but the order is quit different
from when the items where inserted into the hashtable.

Thanks
D.
Dec 13 '06 #4

Brian Gideon wrote:
There's no suitable collection in the BCL (that I know)
Err...well, there is the KeyedCollection abstract class that was added
in 2.0.

Dec 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this?...
4
by: bpdace | last post by:
I have a datasource that is being passed in as a Hashtable. I want to load in all the Values (strings inside the Value, not the Keys) into a collumn inside a DataTable. The values need to be sorted...
5
by: Arjen | last post by:
Hello, Let's say that we have a hashtable with some person objects. This persons have a name. Now I want to sort the people objects inside the hashtable by name. How can the hashtable do this...
9
by: Arjen | last post by:
Hello, Persons is a hashtable which I convert to an array. Person aPerson = new Person; Persons.Values.CopyTo( aPerson, 0 ); Now I can access the person items like aPerson.name or...
2
by: Ali | last post by:
I am binding a hashTable to a dropDownList to pick a State (key: like New York) and sends the state designation (value: NY) to a filtering procedure. I have entered the states in the hashTable in...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
6
by: max sharma | last post by:
Hi all, I am using a hashtable for my application. Its similar to word count application. How can I sort the hashtable w.r.t. the VALUE and not the KEY. The sample data of the table is given below...
2
by: Dimon | last post by:
Hi there, I need to sort multi-dimensional array by two columns. I.e. I have the following: a b c ----------- 1 12 1 0 10 2 1 10 3 1 15 4 0 14 5
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.