473,594 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

IDictionaryEnum erator myEnumerator = myHashTable.Get Enumerator();
while ( myEnumerator.Mo veNext() )
{
//Please print ordered by Key ...
Response.Write( "<BR>" + myEnumerator.Ke y.ToString() + ", " +
myEnumerator.Va lue.ToString())
}
Jul 19 '05 #1
2 20298
"Mark" <fi************ **@umn.edu> wrote in
news:#d******** ******@TK2MSFTN GP11.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

IDictionaryEnum erator myEnumerator = myHashTable.Get Enumerator();
while ( myEnumerator.Mo veNext() )
{
//Please print ordered by Key ...
Response.Write( "<BR>" + myEnumerator.Ke y.ToString() + ", " +
myEnumerator.Va lue.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.co m> wrote in message
news:Xn******** *************** *********@207.4 6.248.16...
"Mark" <fi************ **@umn.edu> wrote in
news:#d******** ******@TK2MSFTN GP11.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

IDictionaryEnum erator myEnumerator = myHashTable.Get Enumerator();
while ( myEnumerator.Mo veNext() )
{
//Please print ordered by Key ...
Response.Write( "<BR>" + myEnumerator.Ke y.ToString() + ", " +
myEnumerator.Va lue.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
7722
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 for me? Thanks!
9
2253
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 aPerson.birthdate.
2
6048
by: RSB | last post by:
How do i sort a Hash Table.. Thanks RSB
3
1481
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 you for your help. Alex using System;
2
5431
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 alphabetical order, but the dropDownList renders them in a non-order list. code snippet: Dim state as New hashTable state.Add("Alabama", "AL") state.Add("Alaska", "AK")
2
571
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? Thanks! -Mark IDictionaryEnumerator myEnumerator = myHashTable.GetEnumerator(); while ( myEnumerator.MoveNext() ) { //Please print ordered by Key ... Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " + myEnumerator.Value.ToString())
6
14664
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 which is sorted according the URL as in Dictionary: www.webroot.com (11) www.webshots.com (1) www.weddingprints.com (4) here, Key = URL (e.g. www.webroot.com)
11
4798
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... ArrayList arr = new ArrayList(); Hashtable row = new Hashtable(); Hashtable row2 = new Hashtable();
1
1302
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 of any way to do it? Usually, I perform these sorts by changing the SQL queries behind the datagrid.
0
7947
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7880
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8242
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5413
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.