views:

1306

answers:

2

I have a List where T is a class that exposes a "Username" property. Username is of a custom type that encapsulates a string. I implemented the IComparable<T> interface on this custom type that simply returns

this.encapsulatedString.CompareTo(other.encapsulatedString)

I defined an ICollectionView of the List thus:

AllUsers=CollectionViewSource.GetDefaultView(myList);

I added a Sortdescription:

AllUsers.SortDrscriptions.Add(new SortDescription("Username",ListSortDirection.Ascending));

On this line the code throws the exception stated in the title. I can sort the list by other means without problem. Where is the exception coming from?

+4  A: 

Stupidstupidstupid: The custom type has to implement IComparable as well as IComparable<T> It seems the SortDescription uses the old fashioned non-generic version of CompareTo

I am going to get some much needed sleep...

Dabblernl
Feel free to close your question if you wish...
Noldorin
NO, don't close. Someone else might run into the same problem
erikkallen
+1 Yeah like me...thanks for keeping it open. :-)
Donny V.
A: 

As you said, you need to implement the non-generic IComparable. You can use the Comparer<T> class if you want to implement this interface in a nice generic way :)

thecoop