Suppose I have a method to return a list of keys in a dictionary, sorted by their values:
/// Perhaps a wildly inefficient way to achieve this!
public static List<K> SortByValues<K,V>( Dictionary<K,V> items )
{
var keys = new K[items.Count];
var values = new V[items.Count];
var index = 0;
foreach( var kvp in items )
{
keys[index] = kvp.Key;
values[index++] = kvp.Value;
}
Array.Sort( values, keys );
return new List<K>( keys );
}
I would like to offer an overload that accepts an IComparer to pass to the Array.Sort
method. Is there any way to do this without duplicating all of the code? Ideally there'd be some way to get at the "default" comparer for the type V.