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

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 20271
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arjen | last post by:
Hello, Let's say that we have a hashtable with some person objects. This persons have a name. Now I want to sort the people objects inside the hashtable by name. How can the hashtable do this...
9
by: Arjen | last post by:
Hello, Persons is a hashtable which I convert to an array. Person aPerson = new Person; Persons.Values.CopyTo( aPerson, 0 ); Now I can access the person items like aPerson.name or...
2
by: RSB | last post by:
How do i sort a Hash Table.. Thanks RSB
3
by: Alexander Widera | last post by:
Hi, I have a problem with this code ... (see below) ... I want to sort an instance of MyList ... by MyData.Shortname ... Shortname is of the type string.... how can I sort the entries? Thank...
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...
2
by: Mark | last post by:
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?...
6
by: max sharma | last post by:
Hi all, I am using a hashtable for my application. Its similar to word count application. How can I sort the hashtable w.r.t. the VALUE and not the KEY. The sample data of the table is given below...
11
by: Cappamontana | last post by:
Hi, I'm stuck with a problem...in .Net C# I want to sort an ArrayList containing Hashtables. The sort key must be the key of the Hashtable. How can I do this? I've coded some example here... ...
1
by: Pedro Rosas Silva | last post by:
Hi all, I have a datagrid in an ASP.Net 1.1 web application which is binded to an HashTable. The grid has several columns and I want to sort the results by one of those columns. Are you aware...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.