Sunday, May 1, 2011

Compare KeyValuePair

KeyValuePair doesn't implement IComparable. If it did, I think it would probably work like this:

public static int CompareKeyValuePair<KeyT, ValueT>
(
  KeyValuePair<KeyT, ValueT> a,
  KeyValuePair<KeyT, ValueT> b
)
  where KeyT : IComparable
  where ValueT : IComparable
{
  int keyCompareResult = a.Key.CompareTo(b.Key);
  
  if (keyCompareResult == 0)
    return a.Value.CompareTo(b.Value);
  else
    return keyCompareResult;
}

No comments:

Post a Comment