469,306 Members | 1,658 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Sort HashTable by Key

I'm using an enumerator to iterate through a HashTable that contains all
numeric keys. I'd like to iterarate through the HashTable based on the
ordered keys. Is there a quick way to do this? Thanks! -Mark

IDictionaryEnumerator myEnumerator = myHashTable.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
//Please print ordered by Key ...
Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " +
myEnumerator.Value.ToString())
}
Jul 19 '05 #1
2 19946
"Mark" <fi**************@umn.edu> wrote in
news:#d**************@TK2MSFTNGP11.phx.gbl:
I'm using an enumerator to iterate through a HashTable that contains
all numeric keys. I'd like to iterarate through the HashTable based
on the ordered keys. Is there a quick way to do this? Thanks! -Mark

IDictionaryEnumerator myEnumerator = myHashTable.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
//Please print ordered by Key ...
Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " +
myEnumerator.Value.ToString())
}


You cannot sort a hashtable. data is not stored in an ordered fashon.

from help:
"When an element is added to the Hashtable, the element is placed into a
bucket based on the hash code of the key. Subsequent lookups of the key
use the hash code of the key to search in only one particular bucket,
thus substantially reducing the number of key comparisons required to
find an element."

values property:
"The order of the values in the ICollection is unspecified, but it is
the same order as the associated keys in the ICollection returned by the
Keys method."

This is why hashTable is faster for key based storage. If you are using
only index based indexers then use an ArrayList or other index based
collection. The ArrayList has a Sort method. The SortedList is always
sorted.

If you require both index and key based indexers then you need to create
your own hybrid collection type. I recommend you derive from
CollectionBase and then add an internal HashTable for storing by key.
CollectionBase already contains an internal ArrayList for storing by
index.

I've just created a sample collections demo project. I was planning on
writing a generator today to create a strongly typed collection of any
type that is accessed by index or key. I will put it on sourceforge.
I'll post a reply to this message when it is complete and available for
download with a link.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/dbobjecter (code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)

Jul 19 '05 #2
Thanks Michael. I look forward to looking at it. -Mark

"Michael Lang" <ml@nospam.com> wrote in message
news:Xn********************************@207.46.248 .16...
"Mark" <fi**************@umn.edu> wrote in
news:#d**************@TK2MSFTNGP11.phx.gbl:
I'm using an enumerator to iterate through a HashTable that contains
all numeric keys. I'd like to iterarate through the HashTable based
on the ordered keys. Is there a quick way to do this? Thanks! -Mark

IDictionaryEnumerator myEnumerator = myHashTable.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
//Please print ordered by Key ...
Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " +
myEnumerator.Value.ToString())
}


You cannot sort a hashtable. data is not stored in an ordered fashon.

from help:
"When an element is added to the Hashtable, the element is placed into a
bucket based on the hash code of the key. Subsequent lookups of the key
use the hash code of the key to search in only one particular bucket,
thus substantially reducing the number of key comparisons required to
find an element."

values property:
"The order of the values in the ICollection is unspecified, but it is
the same order as the associated keys in the ICollection returned by the
Keys method."

This is why hashTable is faster for key based storage. If you are using
only index based indexers then use an ArrayList or other index based
collection. The ArrayList has a Sort method. The SortedList is always
sorted.

If you require both index and key based indexers then you need to create
your own hybrid collection type. I recommend you derive from
CollectionBase and then add an internal HashTable for storing by key.
CollectionBase already contains an internal ArrayList for storing by
index.

I've just created a sample collections demo project. I was planning on
writing a generator today to create a strongly typed collection of any
type that is accessed by index or key. I will put it on sourceforge.
I'll post a reply to this message when it is complete and available for
download with a link.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/dbobjecter (code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Arjen | last post: by
9 posts views Thread by Arjen | last post: by
2 posts views Thread by RSB | last post: by
3 posts views Thread by Alexander Widera | last post: by
2 posts views Thread by Ali | last post: by
2 posts views Thread by Mark | last post: by
6 posts views Thread by max sharma | last post: by
1 post views Thread by Pedro Rosas Silva | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.