473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hashtable ordering

Cannot not seem to make any sense of the order that my key/values end up in
when added to the Hashtable...ide ally, 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...a nd 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....muc h 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 2663
Sigh
Sorry no,
Can you try to have the sorted combo box instead?

--
cheers,
RL
"Tom" <To*@discussion s.microsoft.com wrote in message
news:BC******** *************** ***********@mic rosoft.com...
Cannot not seem to make any sense of the order that my key/values end up
in
when added to the Hashtable...ide ally, 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...a nd 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....muc h 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*@discussion s.microsoft.com wrote in message
news:BC******** *************** ***********@mic rosoft.com...
Cannot not seem to make any sense of the order that my key/values end up
in
when added to the Hashtable...ide ally, 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...a nd 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....muc h 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*@discussion s.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 SortedDictionar y 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
SortedDictionar y, 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...@discussio ns.microsoft.co mwrote:
Cannot not seem to make any sense of the order that my key/values end up in
when added to the Hashtable...ide ally, 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...a nd 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....muc h 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************ *************** *******@microso ft.com>, Tom
wrote:
My goal is to control the ordering...not sort the items.
I ended up using the "System.Collect ions.Specialize d.OrderedDictio nary"
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
5220
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 hashtable by ordering by one of the attributes of the class (a1 or a2). How can this easily be achieved ? I think I have to copy to an ArrayList...
5
2813
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 like it to return an XML serialized version of an object.
5
15568
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. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable...
33
3294
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 used before adding, removing or iterating the Hashtable. The program runs in a high workload environment. After running a few days, now it suddenly...
3
9682
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 atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
9
7245
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 and am not able to get a solution. In the C# code that am working on, there is an arraylist and they are copying the values to a hashtable for...
8
2070
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 = myHashtable.Keys;
2
3139
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 need to write to it, but now it occurred to me that maybe I could just lock one of the Hashtables inside without locking the entire object, but...
4
4750
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 when the items where inserted into the hashtable. Thanks D.
0
7541
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7979
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7826
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6065
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5107
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.