473,569 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hashtable items order

Hello.
I have a Hashtable, to which I insert key and values.
When I go over the hashtable with "foreach" later on,
the items are not coming out at the order I inserted them.
A quickwatch showed that as the items are inserted, they are not arranged in
the hash at the order they were inserted by.

Is there a way to control that with a hashtable ? I can't even sort it...
Thanks
Dec 27 '05 #1
4 25875
Nadav <na****@gmail.c om> wrote:
I have a Hashtable, to which I insert key and values.
When I go over the hashtable with "foreach" later on,
the items are not coming out at the order I inserted them.
No, they wouldn't (necessarily).
A quickwatch showed that as the items are inserted, they are not arranged in
the hash at the order they were inserted by.

Is there a way to control that with a hashtable ? I can't even sort it...


Hashtables aren't inherently ordered - they're maps. You could use
SortedList (which is actually a map, despite the name, sorted by key),
but if you want insertion order, it's probably easiest to keep a
parallel ArrayList.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 27 '05 #2
AFAIK no.

You can create wrapper that will consist of hashtable for fast search and a
list for iteration.
That is when you do myCustHashtable[key] - intrnal hashtable is involved,
and when you do foreach(Key k in myCustHashtable .Keys) - list is involved.

With this approach you have to keep internal hastable and list in
synchronized state.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Nadav" <na****@gmail.c om> wrote in message
news:u8******** ********@tk2msf tngp13.phx.gbl. ..
Hello.
I have a Hashtable, to which I insert key and values.
When I go over the hashtable with "foreach" later on,
the items are not coming out at the order I inserted them.
A quickwatch showed that as the items are inserted, they are not arranged
in
the hash at the order they were inserted by.

Is there a way to control that with a hashtable ? I can't even sort it...
Thanks

Dec 27 '05 #3
Hello Nadav,

Look at System.Collecti ons.Specialized .NameObjectColl ectionBase (ms-help://MS.MSDNQTR.v80. en/MS.MSDN.v80/MS.NETDEVFX.v20 .en/cpref2/html/T_System_Collec tions_Specializ ed_NameObjectCo llectionBase.ht m)

There is an example how to build smth like Hashlist - to keep keys/values
and get them by order

It should helps

N> I have a Hashtable, to which I insert key and values.
N> When I go over the hashtable with "foreach" later on,
N> the items are not coming out at the order I inserted them.
N> A quickwatch showed that as the items are inserted, they are not
N> arranged in
N> the hash at the order they were inserted by.
N> Is there a way to control that with a hashtable ? I can't even sort
N> it... Thanks
N>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Dec 27 '05 #4
If you insist on using a Hashtable (which you might, because insertion
speed is a lot faster than for SortedList), the only way you can sort
is to do a ToArray() and then sort the resulting array on demand.

However, as Jon pointed out, there is SortedList, which is probably
what you want.

Dec 28 '05 #5

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

Similar topics

0
341
by: choyk1 | last post by:
I intended to save properties of an object to a Hashtable. In this case, keys and values are not fixed types and I cannot use SortedList. I have no idea what order the .NET framework add items to Hashtable when Add(key, value) method is applied. Monitoring the position items are added, I cannot find if key is the standard or value. I thought...
1
330
by: frazer | last post by:
hi i have a hashtable which has some items in it. when i enumerate it using the IDictionaryEnumerator . it gives me the items in reverse order. why is that. how do i correct it? thnx
3
9683
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");
11
2747
by: Crirus | last post by:
Easyest and fastest way... :) -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------
2
5428
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 alphabetical order, but the dropDownList renders them in a non-order list. code snippet: Dim state as New hashTable state.Add("Alabama", "AL")...
17
5067
by: John A Grandy | last post by:
For a Hashtable that is expected to contain no more than 100 DictionaryEntry elements , what is the best load factor ? ( This Hashtable is a encapsulted in a class , an instance of which is cached and methods on which are called by n threads ( n is approx = 25 ) )
5
2665
by: =?Utf-8?B?VG9t?= | last post by:
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...
5
2879
by: rk2008 | last post by:
I am using Hashtable to keep Key-Value pair of elements. When I add the items to the Hashtable it does not retain the order in which I have added the key-value pair. Is there anyway to retain the order?
1
1894
by: chuckroc76 | last post by:
I am running into an issue with a HashTable where it isnt holding data in the order that I am adding it. I am pulling a SqlDataReader out of a table and looping through that to add the items to the HashTable. I have an ID field (autonumber) and a Sort Index field. I want the items to be stored in the hashtable by Sort Index. However, when I add...
1
7678
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
7982
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
6286
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...
0
5222
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...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.