How do I use the IComparable interface?
问题 I need a basic example of how to use the IComparable interface so that I can sort in ascending or descending order and by different fields of the object type I'm sorting. 回答1: Well, since you are using List<T> it would be a lot simpler to just use a Comparison<T> , for example: List<Foo> data = ... // sort by name descending data.Sort((x,y) => -x.Name.CompareTo(y.Name)); Of course, with LINQ you could just use: var ordered = data.OrderByDescending(x=>x.Name); But you can re-introduce this in