By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,157 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,157 IT Pros & Developers. It's quick & easy.

How to sort Hashtable by value and not key

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a


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

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.