473,405 Members | 2,160 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,405 software developers and data experts.

How to sort Hashtable by value and not key

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 please help me.
Thank you all in advance,
Max

*** Sent via Developersdex http://www.developersdex.com ***
Apr 1 '06 #1
6 23530
max sharma <ma********@yahoo.com> wrote:
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 please help me.
Thank you all in advance,


Use a second map, a SortedList, which stores the pairs the other way
round - so when you add an entry, you'd do something like:

normalMap[key] = value;
reverseMap[value] = key;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 1 '06 #2


max sharma wrote:
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 please help me.


If you only need to sort once, something like:

string[] keys = new string[dict.Count];
dict.Keys.CopyTo(keys, 0);
double[] values = new double[dict.Count];
dict.Values.CopyTo(values, 0);
Array.Sort(values, keys);

If you need to access the data sorted by values while mutating the
hashtable you can implements your own datastructure which maintains: a
hash from string to double, and a sorted-list which maps the doubles to
strings. Be sure to delete the right values from the sorted-list if
duplicate doubles occur though.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Apr 1 '06 #3
Hi Max,

Did you check the Wintellect OrderedDictionary ? You can find the
code on www.wintellect.com under Power Collections for .Net. Don't
forget to create an account.

Regards,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Apr 1 '06 #4


ba**********@hotmail.com wrote:
Hi Max,

Did you check the Wintellect OrderedDictionary ? You can find the
code on www.wintellect.com under Power Collections for .Net. Don't
forget to create an account.


"Register here to get look at any stuff" -- Nah, I think I'll pass on that.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Apr 2 '06 #5
Thank you all for giving me such valuabe information...
I needed the solution quick so I did it on my own. I used a string[,]
array to store the key-value pairs and sorted this array based on values
using heap sort. It works and it pretty
fast considering the amount of data I have.
Thank you all once again.
Max


*** Sent via Developersdex http://www.developersdex.com ***
Apr 7 '06 #6
Thank you all for giving me such valuabe information...
I needed the solution quick so I did it on my own. I used a string[,]
array to store the key-value pairs and sorted this array based on values
using heap sort. It works and it pretty
fast considering the amount of data I have.
Thank you all once again.
Max
*** Sent via Developersdex http://www.developersdex.com ***
Apr 7 '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 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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.