HowTo: implement a list that sorts based on value ( not key ! )
By rickvdbosch
- 3 minutes read - 433 words<div>
<font face="Courier New"> <font color="#0000ff">private string</font> theKey;</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">private string</font> theValue;</font>
</div>
<div>
<font face="Courier New"> </font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">public string</font> Key</font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">get</font></font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">return</font> theKey;</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New"> </font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">public string</font> Value</font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">get</font></font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">return</font> theValue;</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New"> </font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">public</font> KeyValuePair(<font color="#0000ff">string</font> key, <font color="#0000ff">string</font> value)</font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> theKey = key;</font>
</div>
<div>
<font face="Courier New"> theValue = value;</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New"> </font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">public int</font> CompareTo(<font color="#0000ff">object</font> obj)</font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">if</font> (obj == <font color="#0000ff">null</font>)</font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">return</font> 1;</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">else</font></font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> KeyValuePair check = (KeyValuePair)obj;</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">return</font> Value.CompareTo(check.Value);</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<div>
<font face="Courier New">}</font>
</div>
<div>
</div>
<div>
Now when you want a value-sorted list, you can simply create an <a href="https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsarraylistclasstopic.asp">ArrayList</a>, add KeyValuePairs to the ArrayList, call the Sort() method (which calls the CompareTo() method for each of the objects that exist in the ArrayList) and see that the items in your ArrayList are sorted based on the values of all KeyValuePairs!!!</p>
<p>
Sample:
</p>
</div>
<div>
<font face="Courier New"> ArrayList arrayList;</font>
</div>
<div>
<font face="Courier New"> arrayList = <font color="#0000ff">new</font> ArrayList();</font>
</div>
<div>
<font face="Courier New"> arrayList.Add(<font color="#0000ff">new</font> KeyValuePair(<font color="#800080">"1"</font>, <font color="#800080">"bbb"</font>));</font>
</div>
<div>
<font face="Courier New"> arrayList.Add(<font color="#0000ff">new</font> KeyValuePair(<font color="#800080">"2"</font>, <font color="#800080">"ccc"</font>));</font>
</div>
<div>
<font face="Courier New"> arrayList.Add(<font color="#0000ff">new</font> KeyValuePair(<font color="#800080">"3"</font>, <font color="#800080">"aaa"</font>));</font>
</div>
<div>
<font face="Courier New"> arrayList.Sort();</font>
</div>
<div>
<font face="Courier New"> <font color="#0000ff">foreach</font> (KeyValuePair keyValuePair <font color="#0000ff">in</font> arrayList)</font>
</div>
<div>
<font face="Courier New"> {</font>
</div>
<div>
<font face="Courier New"> proofTextBox.Text += keyValuePair.Key + <font color="#800080">" : "</font> + keyValuePair.Value + Environment.NewLine;</font>
</div>
<div>
<font face="Courier New"> }</font>
</div>
<p>
</span>
</p>