Hashtables

  • Thread starter Thread starter Softwaremaker
  • Start date Start date
S

Softwaremaker

Hi all,

I am using a hashtable to store some values. However, I am not able to sort
the values in the hashtable based on an ascending order.

Seems to me that the hashtable can only sort the hash index of the values.
Is that true ?

Thank you.
 
Hi,

That is correct the Hashtable can't sort the values. However you can use the
Values property of the Hashtable to get a collection of the values and then
create a ArrayList on the collection and sort it.

ArrayList a = new ArrayList( hash.Values ); // Create an ArrayList based on
the Hashtable values
a.Sort(); // Sort the ArrayList
....

Hope this helps

Chris Taylor
 
Hello William,

Thanks for posting in the group.

Chris and You are right. HashTable can't sort the values by itself. For
this issue, I suggest you use SortedList. A SortedList is a hybrid between
a Hashtable and an Array. When an element is accessed by its key using the
Item indexer property, it behaves like a Hashtable. When an element is
accessed by its index using GetByIndex or SetByIndex, it behaves like an
Array.

In SortedList, the index sequence is based on the sort sequence. When an
element is added, it is inserted into SortedList in the correct sort order,
and the indexing adjusts accordingly. When an element removed, the indexing
also adjusts accordingly. Therefore, the index of a specific key-and-value
pair might change as elements are added or removed from the SortedList.

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: "Softwaremaker" <[email protected]>
!Subject: Hashtables
!Date: Wed, 3 Sep 2003 07:55:47 +0800
!Lines: 17
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <[email protected]>
!Newsgroups: microsoft.public.dotnet.framework
!NNTP-Posting-Host: bb220-255-10-104.singnet.com.sg 220.255.10.104
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:52817
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!Hi all,
!
!I am using a hashtable to store some values. However, I am not able to sort
!the values in the hashtable based on an ascending order.
!
!Seems to me that the hashtable can only sort the hash index of the values.
!Is that true ?
!
!Thank you.
!
!--
!William T
!Software Architect / Chief Software Developer
!Softwaremaker.Net Pte Ltd
!http://www.Softwaremaker.Net
!
!
!
 
Thanks guys, I appreciate the response. Will make the necessary changes.

Thank you.
 
Back
Top