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

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 25756
Nadav <na****@gmail.com> 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.com>
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.com> wrote in message
news:u8****************@tk2msftngp13.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.Collections.Specialized.NameObjectCollectio nBase (ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/T_System_Collections_Specialized_NameObjectCollect ionBase.htm)

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
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...
1
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
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...
11
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
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...
17
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...
5
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....
5
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.