473,396 Members | 1,836 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,396 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 14650
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 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...
9
by: Oberon | last post by:
My HashTable (Global.Games) is a static collection of objects of type Game. A Game object has 8 fields (exposed as properties). The key to the HashTable is also one of these fields (GameID, of type...
4
by: Arjen | last post by:
Hi, I need to add this inside an array: 1 3 2 4 3 2 4 5 5 1 I think of using this:
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...
2
by: Ali | last post by:
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...
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 hashtable in C# with keys of type string and values of type double. I am not able to find a way to sort them by value (descending to be precise) instead of key. Can someone...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.