471,319 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

how ti sort hashtable by Value

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))
Thank you, please reply asap,
MAX

*** Sent via Developersdex http://www.developersdex.com ***
Feb 11 '06 #1
6 14525
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.

"max sharma" <ma********@yahoo.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
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))
Thank you, please reply asap,
MAX

*** Sent via Developersdex http://www.developersdex.com ***

Feb 11 '06 #2
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.

Feb 12 '06 #3
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.

Feb 12 '06 #4
Max,

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"max sharma" <ma********@yahoo.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
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))
Thank you, please reply asap,
MAX

*** Sent via Developersdex http://www.developersdex.com ***

Feb 12 '06 #5
"max sharma" <ma********@yahoo.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
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.
Feb 12 '06 #6


max sharma wrote:
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[i], values[i]);

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.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Feb 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark | last post: by
5 posts views Thread by Arjen | last post: by
9 posts views Thread by Arjen | last post: by
4 posts views Thread by Arjen | last post: by
3 posts views Thread by Alexander Widera | last post: by
2 posts views Thread by Ali | last post: by
6 posts views Thread by max sharma | last post: by
reply views Thread by rosydwin | last post: by

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.