C# 2.0: Sorting a Hashtable by Values<span style="font-family:arial">Hi!<br><br>I have a hashtable in a .NET2.0 project such that:</span><br><br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>Hashtable myHT = new Hashtable();<br>myHT.Add(&quot;Bob&quot;,  23);<br>myHT.Add(&quot;Jane&quot;, 14);<br>myHT.Add(&quot;Dave&quot;,  7);<br>myHT.Add(&quot;Mike&quot;, 18);</p></div></div><br> <p><br>I would like to sort myHT by the values, and not the keys.  How would I go about doing that?  I've used IComparer before in .NET 1.1, but it appears to be obsolete in .NET2.0.  Can anybody offer some help?  Thanks in advance.</p> <p><br></p> <p><br></p>© 2009 Microsoft Corporation. All rights reserved.Wed, 08 Apr 2009 21:25:21 Z94ae3545-dfbc-40a8-b083-4f426047d8fdhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#94ae3545-dfbc-40a8-b083-4f426047d8fdhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#94ae3545-dfbc-40a8-b083-4f426047d8fd_Bullineshttp://social.msdn.microsoft.com/Profile/en-US/?user=_BullinesC# 2.0: Sorting a Hashtable by Values<span style="font-family:arial">Hi!<br><br>I have a hashtable in a .NET2.0 project such that:</span><br><br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>Hashtable myHT = new Hashtable();<br>myHT.Add(&quot;Bob&quot;,  23);<br>myHT.Add(&quot;Jane&quot;, 14);<br>myHT.Add(&quot;Dave&quot;,  7);<br>myHT.Add(&quot;Mike&quot;, 18);</p></div></div><br> <p><br>I would like to sort myHT by the values, and not the keys.  How would I go about doing that?  I've used IComparer before in .NET 1.1, but it appears to be obsolete in .NET2.0.  Can anybody offer some help?  Thanks in advance.</p> <p><br></p> <p><br></p>Thu, 07 Jun 2007 20:50:58 Z2007-06-11T07:17:20Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#ce5d1a26-e613-4be8-b0be-ca006fa554behttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#ce5d1a26-e613-4be8-b0be-ca006fa554beBruno_1http://social.msdn.microsoft.com/Profile/en-US/?user=Bruno_1C# 2.0: Sorting a Hashtable by ValuesJust ignore or remove the warning, and implement IComparer, it is the simpler way to do it.I don't think that you can solve directly this with only using a hashtable without the IComparer.<br>Thu, 07 Jun 2007 22:00:55 Z2007-06-07T22:00:55Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#0aecd655-7032-4e0d-acf2-b8451bff060chttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#0aecd655-7032-4e0d-acf2-b8451bff060cAndreas Johanssonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Andreas%20JohanssonC# 2.0: Sorting a Hashtable by Values<p>You might want to have a look at <a title="http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx" href="http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx">Dictionary&lt;T&gt;</a> as it is the hashed table to use in .NET 2.0.</p> <p> </p> <p>What was your approach to sort after values before?</p>Thu, 07 Jun 2007 23:52:44 Z2007-06-07T23:52:44Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#66493789-6ab1-4624-89c3-00c41a29d965http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#66493789-6ab1-4624-89c3-00c41a29d965Bruno_1http://social.msdn.microsoft.com/Profile/en-US/?user=Bruno_1C# 2.0: Sorting a Hashtable by ValuesDictionary&lt;&gt; is worst than Hashtable for sorting, because they don't even bother themself by putting the IComparer interface for comparing. Could you tell us how you sorted the hashtable by value before ?<br><br>Fri, 08 Jun 2007 05:34:31 Z2007-06-08T05:34:31Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#d03cf006-dbb5-453e-ba82-f896d48993c6http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#d03cf006-dbb5-453e-ba82-f896d48993c6OmegaManhttp://social.msdn.microsoft.com/Profile/en-US/?user=OmegaManC# 2.0: Sorting a Hashtable by Values<div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>bruno_1 wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%">Dictionary&lt;&gt; is worst than Hashtable for sorting, because they don't even bother themself by putting the IComparer interface for comparing. </td></tr></tbody></table></td></tr></tbody></table></div><br><br>Well yes and no, HashTable had to be the swiss army knife of operations. Dictionary is leaner. A dictionary is only for storing values with a key...there is no need to sort the whole thing...just its keys or values....<br><br>The keys and values can be viewed as List&lt;t&gt;. The List&lt;t&gt; is created from the ICollection returned by the exposed properties on the Dictionary of Keys/Values.<br><br>Here I extract the values, I could have done the keys, and sorted them.<br><br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>Dictionary&lt;int, string&gt; myDict = new Dictionary&lt;int, string&gt;();<br>myDict.Add(2, &quot;This&quot;);<br>myDict.Add(1, &quot;is&quot;);<br>myDict.Add(5, &quot;radio&quot;);<br>myDict.Add(4, &quot;clash&quot;);<br><br>List&lt;string&gt; song = new List&lt;string&gt;(myDict.Values);<br>song.Sort();<br><br>// This writes out: &quot;clash is radio This&quot;<br>Console.WriteLine(string.Join(&quot; &quot;, song.ToArray()));</p></div></div><br>Fri, 08 Jun 2007 22:05:59 Z2007-06-11T07:17:20Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#381903f6-b156-4fe5-8b5b-7fb6a20f2ee2http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#381903f6-b156-4fe5-8b5b-7fb6a20f2ee2nobugzhttp://social.msdn.microsoft.com/Profile/en-US/?user=nobugzC# 2.0: Sorting a Hashtable by Values<font size=2><span style="font-family:Verdana,Geneva,Arial,Sans-serif">System.Collections.Generic.SortedDictionary would be the logical choice.  One of its constructors takes an IComparer&lt;T&gt; as its argument.  Yell if you need sample code.<br></span></font>Sat, 09 Jun 2007 20:04:39 Z2007-06-11T07:05:28Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#54f7b6d4-2c82-4205-9014-182c40c7f445http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#54f7b6d4-2c82-4205-9014-182c40c7f445OmegaManhttp://social.msdn.microsoft.com/Profile/en-US/?user=OmegaManC# 2.0: Sorting a Hashtable by Values<div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>Chris Bellini wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%">I would like to sort myHT by the values, and not the keys. How would I go about doing that? </td></tr></tbody></table></td></tr></tbody></table></div><br><br> <div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>nobugz wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"><font size=2><span style="font-family:Verdana,Geneva,Arial,Sans-serif">System.Collections.Generic.SortedDictionary would be the logical choice. One of its constructors takes an IComparer&lt;T&gt; as its argument. Yell if you need sample code.<br></span></font></td></tr></tbody></table></td></tr></tbody></table></div><br><br>Unless I am mistaken Hans, SortedDictionary only sorts on keys and not values:<br><br> <div style="margin-left:40px">&quot;<span style="font-style:italic">Represents a collection of key/value pairs that are sorted on the key.</span>&quot; - <a title="http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx" href="http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx">SortedDictionary Class MSDN</a><br></div>Wed, 13 Jun 2007 20:34:27 Z2007-06-13T20:34:27Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#26111bd3-f4a0-4c2e-851a-3dc530fd8f77http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#26111bd3-f4a0-4c2e-851a-3dc530fd8f77Bruno_1http://social.msdn.microsoft.com/Profile/en-US/?user=Bruno_1C# 2.0: Sorting a Hashtable by ValuesYou're right this is why we asked the poster how he already did it with the hashtable, cause hashtable has similar functionality for the IComparer&lt;T&gt;, and it doesn't provide sorting by value.  And I think it shouldn't because the insertion is done by key(IComparer&lt;T&gt; is on the key). anyway, you can not sort in the same time by key and value.<br>However, it is a good idea to mention the SortedDictionary&lt;&gt;, but i think that the solution can not be done just by one collection, there is also the SortedList&lt;Tkey, TValue&gt; wich is less efficient in inserting and removing, but much better for memory consumption than SortedDictionary.<br>Wed, 13 Jun 2007 21:39:09 Z2007-06-13T21:39:09Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#e498828f-3989-496b-89df-64b5983e6152http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#e498828f-3989-496b-89df-64b5983e6152nobugzhttp://social.msdn.microsoft.com/Profile/en-US/?user=nobugzC# 2.0: Sorting a Hashtable by Values<font size=2><span style="font-family:Verdana,Geneva,Arial,Sans-serif">The <a title="http://www.wintellect.com/PowerCollections.aspx" href="http://www.wintellect.com/PowerCollections.aspx">Wintellect Power Collections</a> have many OrderedSomething collections.  Something ought to fit your bill.<br></span></font>Wed, 13 Jun 2007 23:49:56 Z2007-06-13T23:49:56Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#b8c74690-7bae-48fd-9312-d80ae46c31f6http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/94ae3545-dfbc-40a8-b083-4f426047d8fd#b8c74690-7bae-48fd-9312-d80ae46c31f6MeMyselfAndIhttp://social.msdn.microsoft.com/Profile/en-US/?user=MeMyselfAndIC# 2.0: Sorting a Hashtable by Values<p>I realize that is thread is 6 months old.  But, since I did not find a newer thread on the topic and I found a solution, I decided to post it.</p> <p align=left> </p> <p align=left>I had the same problem of needing to have a sorted pair of values.  I had used a hashtable to help build these values fas a summary of a very large collection.  I then needed to get the pairs with highest summary first.  I found the KeyValuePair in the Generics namespace to be very useful.  But to use KeyValuePair, I changed my hashtable to a dictionary.</p> <p align=left> </p><font size=2> <p></font><font color="#2b91af" size=2>Dictionary</font><font size=2>&lt;</font><font color="#0000ff" size=2>string</font><font size=2>, </font><font color="#0000ff" size=2>int</font><font size=2>&gt; dict = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>Dictionary</font><font size=2>&lt;</font><font color="#0000ff" size=2>string</font><font size=2>, </font><font color="#0000ff" size=2>int</font><font size=2>&gt;();</font></p> <p align=left><font size=2></font> </p> <p align=left><font size=2>/*  populate dictionary */</font></p> <p align=left><font size=2></font> </p><font size=2><font size=2> <p></font><font color="#0000ff" size=2><font color="#2b91af">List</font><font color="#000000" size=2>&lt;</font><font color="#2b91af" size=2>KeyValuePair</font><font color="#000000" size=2>&lt;</font><font color="#0000ff" size=2>string</font><font color="#000000" size=2>, </font><font color="#0000ff" size=2>int</font><font color="#000000" size=2>&gt;&gt; </font>summaryList </font><font size=2>= </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>List</font><font size=2>&lt;</font><font color="#2b91af" size=2>KeyValuePair</font><font size=2>&lt;</font><font color="#0000ff" size=2>string</font><font size=2>, </font><font color="#0000ff" size=2>int</font><font size=2>&gt;&gt;();</font></p> <p align=left><font size=2></font> </p><font size=2><font size=2> <p></font><font color="#0000ff" size=2>summaryList</font><font size=2>.AddRange(dict);</p> <p></font><font color="#0000ff" size=2>summaryList</font><font size=2>.Sort</font></p> <p align=left><font size=2>   (</p> <p></font><font color="#0000ff" size=2>      delegate</font><font size=2>(</font><font color="#2b91af" size=2>KeyValuePair</font><font size=2>&lt;</font><font color="#0000ff" size=2>string</font><font size=2>, </font><font color="#0000ff" size=2>int</font><font size=2>&gt; kvp1,</p> <p></font><font color="#2b91af" size=2>                   KeyValuePair</font><font size=2>&lt;</font><font color="#0000ff" size=2>string</font><font size=2>, </font><font color="#0000ff" size=2>int</font><font size=2>&gt; kvp2)</p> <p>         { </p> <p></font><font color="#0000ff" size=2>            return</font><font size=2> </font><font color="#2b91af" size=2>Comparer</font><font size=2>&lt;</font><font color="#0000ff" size=2>int</font><font size=2>&gt;.Default.Compare(kvp1.Value, kvp2.Value);</p> <p>         }</p> <p>    );</p></font></font></font> <p align=left><font face=Arial size=2></font> </p>Mon, 17 Dec 2007 22:42:46 Z2007-12-17T22:42:46Z