By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

Hashtable items order

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.