Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Hashtable by (possibly non-unique) values

Tags:

c#

hashtable

I have a Hashtable that maps strings to ints. Strings are unique, but several may be mapped to the same integer.

My naive approach was to simply invert the Hashtable to a SortedList that is indexed by the Hashtable's values, but the problem is that you get a clash as soon as two of the Hashtable's strings map to the same value.

What is the most efficient way to list my entire Hashtable (keys and values) ordered by the values? (Where two values are the same, I don't care about their ordering.)

like image 707
mollmerx Avatar asked Dec 16 '22 23:12

mollmerx


1 Answers

Using Linq:

hashtable.Cast<DictionaryEntry>().OrderBy(entry => entry.Value).ToList()
like image 129
Quartermeister Avatar answered Jan 02 '23 16:01

Quartermeister