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

Sort HashTable by Key

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


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

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