How do I get sorted key/value pairs from a hastable

G

Guest

Hi,

I have a hastable with key/value pairs. I want to iterate thru the
collection in descending order by value. What is the 'best practice' for
handling this problem?

Thanks in advance for your help,
Hal Heinrich
VP Technology
Aralan Solutions Inc.
 
J

Jay B. Harlow [MVP - Outlook]

Hal,
Consider using a System.Collections.SortedList instead of a HashTable.

The SortedList represents "a collection of key-and-value pairs that are
sorted by the keys and accessible by key and by index". In other words its
an ordered HashTable.

Unfortunately the SortedList will only get sort the keys & not the values.

If you want sorted values, I would put all the values into an ArrayList,
then sort the ArrayList in descending order (using a custom IComparable
object). If my app needed to "repeatedly" do this sort I would consider
creating a custom collection that maintained both the index (the descending
value arraylist) and the original hashtable...

Post if you need samples.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top