Hashtable ordering

G

Guest

Cannot not seem to make any sense of the order that my key/values end up in
when added to the Hashtable...ideally, I would like to be able to sort the
keys/values...but not thinking it is possible.

For those who are sure going to ask why I am asking this....I use the
Hashtable for conveniently cross-referencing pairs of information...and I am
now trying to populate a combo box based on the hashtable contents using the
key as the combo box item, and the value as the referenced information when
that item is selected....much like the DisplayMember/ValueMember when loading
the combo box from an ADO.net data source. My problem here is simply a
visual issue with controlling the order items appear in the combo box.

If anyone knows how to control the order when adding to the Hashtable,
please help! Or...if someone can provide a short explanation of why it
cannot be done, that would sure satisfy my curiosity.

Thank you.
 
P

Peter Duniho

My goal is to control the ordering...not sort the items.

I think I know what you mean. However, I find the above comment on the
face of it to be oxymoronic. After all, what does sorting do, if not
"control the ordering"?

Anyway...

I think it's likely that the Hashtable class is ordering the members by
the hashed value. This would allow for faster searching by hash value.
Because of this, it seems to me that you cannot depend on the order within
the Hashtable collection to populate your combo box.

I think you can either use the SortedDictionary class, which should
provide the functionality you need (an ordered collection with efficient
key/value retrieval), or you can explicitly sort a seperate list of your
items before adding them to the list box.

Pete
 
A

atlaste

Hash tables are unsorted by design. Well, they're sorted by
(Key.hash_code % some_number) where the number is usually a prime and
usually is larger than the number of items in the table. The reason
for is that the distribution of the numbers that result from this
calculation is more or less uniformly distributed, which is good for
lookups. That's how these things work. Modifying the ordering might
make the thing work -- but also means you are ruining the hash table
itself. So, there's why it cannot be done.

There are other data structures that allow ordering, like
SortedDictionary, but that results in a performance penalty. Which is
okay in your case.

Search wikipedia for 'rb-tree' (red-black tree) and 'hash table' if
you want to know more about the details.

Cheers,
Stefan.
 
M

Mike Blake-Knox

Tom said:
My goal is to control the ordering...not sort the items.

I ended up using the "System.Collections.Specialized.OrderedDictionary"
to meet a similar need.

Incidentally, I was asked to explain how this can be ordered but not
sorted! My answer was that it is sorted by default - sorted in
insertion order but that you can explicitly determine the order where a
newly inserted object goes.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top