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

Hashtable ordering

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.
May 1 '07 #1
5 2652
Sigh
Sorry no,
Can you try to have the sorted combo box instead?

--
cheers,
RL
"Tom" <To*@discussions.microsoft.comwrote in message
news:BC**********************************@microsof t.com...
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.

May 1 '07 #2
My goal is to control the ordering...not sort the items.

"Egghead" wrote:
Sigh
Sorry no,
Can you try to have the sorted combo box instead?

--
cheers,
RL
"Tom" <To*@discussions.microsoft.comwrote in message
news:BC**********************************@microsof t.com...
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.


May 1 '07 #3
On Tue, 01 May 2007 16:01:01 -0700, Tom <To*@discussions.microsoft.com>
wrote:
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
May 1 '07 #4
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.
On 2 mei, 00:35, Tom <T...@discussions.microsoft.comwrote:
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.

May 2 '07 #5
In article <DB**********************************@microsoft.co m>, Tom
wrote:
My goal is to control the ordering...not sort the items.
I ended up using the "System.Collections.Specialized.OrderedDiction ary"
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

May 5 '07 #6

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

Similar topics

4
by: JezB | last post by:
I have a hashtable (h) of class objects keyed by a string (k) - the class (c) object stored in each hastable entry has two attributes (a1 and a2). I want to iterate through the entries of the...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
9
by: nithya4u | last post by:
Hi All, Currently, am working on converting some part of C# code to JAVA. Am having a problem with respect to the usage of Hashtable in C#. Is there a java equivalent for it? I have searched lots...
8
by: Martin Pöpping | last post by:
Hello, I´ve implemented a Hashtable with Int-Keys and Double Values. Now I want to get the i-th Int-Key of my hashtable. How do I do that? I tried it like that: ICollection intKeys =...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
4
by: Dahab | last post by:
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.