Problem with sorting a generic List
I've got a List<KeyValueType<TKey, TValue>>, so this is a list containing another generic type. Now I would like to sort this list by the TKey type of the KeyValueType<TKey, TValue> type. The list contains KeyValueType objects with in which the TKey is unique.
I've been working on it all night, but can't get the syntax right. I tried to write a class that implements the IComparer<KeyValueType<TKey, TValue>> interface, but the compiler reports errors in my code.
How should this be written?
PS. here is some incorrect code that maybe clarifies what I'm doing:
publicclass BinTree { publicvoid AddToTree(List<KeyValuePair<TKey, TValue>> myList) { Sort the List so it can be added to the treein a balanced way myList.Sort(new MyComparer<KeyValuePair<TKey, TValue>>() ); code to add to tree here. } } Compiler Error CS0081: Type parameter declaration must be an identifier not a type. publicclass MyComparer<KeyValuePair<TKey, TValue>> where TKey : IComparer<KeyValuePair<TKey, TValue>>, IComparable<TKey> { #region IComparer<KeyValuePair<TKey,TValue>> Members publicint Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) { return x.Key.CompareTo(y.Key); } #endregion } |

