how ti sort hashtable by Value

  • Thread starter Thread starter max sharma
  • Start date Start date
I don't think hashtables have any concept of order so sorting does not make
sense. You may need to extract them to a normal list and sort that way.
 
I would create a new collection class that uses a Hashtable and another
collection, such as a sorted ArrayList, internally. Then you can handle
this two ways:

1. When you add a new item to your collection class, add it to the
Hashtable (key, value) and to the ArrayList in value order (using an
insertion sort). When you want to iterate over the sorted items, just
iterate over the sorted ArrayList. This makes insertions expensive, but
sorted reads cheap, and makes sense if you have few insertions but many
reads.

2. Use a Hashtable internally, and when you get a request for the
sorted list, copy the contents of the Hashtable into an array and sort
the array. This makes insertions very cheap, but sorted reads more
expensive, and makes sense if you have frequent insertions.
 
Max,

The best way to do this is to use a SortedList. There is a generic and
non-generic one available.

Hope this helps.
 
A third way is a hybrid of the two: Your class maintains the Hashtable
and array internally, plus a "dirty" flag. Whenever anyone inserts into
the Hashtable, set the "dirty" flag. Whenever a call is made for the
sorted list, sort the Hashtable into the array only if the "dirty" flag
is set. If it's not set, then the array is already complete and in
order.
 
How can I sort the hashtable

As others have said, if you want a sortable Hashtable you'll have to roll
your own...

Otherwise, use a SortedList - that's what it's for.
 
max said:
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)
Value = Count (e.g. (11))

The easiest way is to copy the items to arrays:

Key[] keys = new Key[dict.Count];
dict.Keys.CopyTo(keys, 0);
Value[] values = new Values[dict.Count];
dict.Values.CopyTo(values, 0);

Now, you can sort by the values using System.Array.Sort that manipulates
two arrays at a time, one to sort after, and one that is rearranged like
the one that's sorted after:

System.Array.Sort(values, keys);

Now, the content of keys and values is sorted by values, so to write the
keys in sorted order:

for( int i = 0; i < values.count; ++i )
Console.WriteLine("{0}: {1}", keys, values);

Note that a SortedList won't help you, since you wish to sort on the Values.
Thank you, please reply asap,

Asking for asap anwsers probably won't make people answer faster.
 

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

Back
Top