473,405 Members | 2,141 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,405 software developers and data experts.

Sort a HashTable

Ali
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")
...
state.Add("Wyoming", "WY")
state.Add("Yokon Territory", "YT")

dropDownListState.DataSource = state.keys
dropDownListState.DataBind()

Question 1:
Why does the hashTable items order change in the DropDownList?

Question 2:
Can I sort a hashTable based on the key?

Question 3:
How is a sortedList different from a hashTable?

I am assuming that the hashTable is sorted on the hash, but I am not sure.

Thanks.
Ali
Nov 20 '05 #1
2 5387
Cor
Hi Ali,

This is a hash table
Represents a collection of key-and-value pairs that are organized based on
the hash code of the key.

Why do yo not take a collection that is based on the real value of the key
when you want it in that order?

Cor
Nov 20 '05 #2
Hi

I would recommend using a ListDictionary (in
System.Collections.Specialized) if the amount of data is small, and
you need it ordered the way you added it, e.g adding sequential dates
to a dropdownlist (C# code but hope you get the point):

<...>
ListDictionary dates = new ListDictionary();
for (int i = 0; i <= 10; i++)
{
dates.Add(DateTime.Now.AddDays((double)i).ToLongDa teString(),i);
}

ddlStartdate.DataSource = dates;
ddlStartdate.DataBind();

<...>

will render something like:

<select name="ddlStartdate" id="ddlStartdate">
<option value="0">27. february 2004</option>
<option value="1">28. february 2004</option>
<option value="2">29. february 2004</option>
<option value="3">1. march 2004</option>
<option value="4">2. march 2004</option>
<option value="5">3. march 2004</option>
<option value="6">4. march 2004</option>
<option value="7">5. march 2004</option>
<option value="8">6. march 2004</option>
<option value="9">7. march 2004</option>
<option value="10">8. march 2004</option>
</select>

In this case it might be easier to use a SortedList though.

Hope this helps.

/Michael
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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?...
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...
3
by: Jim Adams | last post by:
I'm counting the frequency of word occurances and would like to return a key/value list sorted descending by frequency. So I need to quickly see if a word (key) is in the list, but later sort...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
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
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...
0
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...

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.