Possible Duplicate:
Sort a Custom Class List<>
I have a Generic Collector List and some code adding CmsCategory in an unordered way to the Collector. CmsCategory has fields int CategoryId and string Title.
List<CmsCategory> categories = new List<CmsCategory>();
categories.Sort();
After the insertion is completed I need to to get all objects in the collector sorted by CategoryId (ascending).
I receive this error:
At least one object must implement IComparable.
My questions:
-
What I’m doing wrong here using
List<T>? -
Do you know more appropriate Generic Collector for this kind of operations?
Thanks for your time on this.
You can use the
OrderBy()method to sort based on a property:If you need the items in a list, you can call
.ToList():The cool thing about using
OrderBy()is that you can also useThenBy()with it to sort based on a second (or third, or fourth…) property as well: