sort order when iterating a hashtable

  • Thread starter Thread starter Dahab
  • Start date Start date
D

Dahab

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.
 
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
 
Hi,

You may also want to consider SortedList<T, K> if your results are sorted to
begin with.
 
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
 
Back
Top