I am trying to figure out the correct way to sort a generic list of objects. A quick example of my data structure:
// The base class
public abstract class Item : IComparable<Item> {
public enum Category { Hats, Shirts, ... }
public Category category;
public int CompareTo (Item that) {
...
}
}
// One of several classes extending Item
public class Hat : Item {
public int CompareTo (Hat that) {
...
}
}
I have a manager class that maintains lists for each class that extends Item:
Dictionary<Item.Category, List<Item>> _items;
...
foreach (Item.Category category in Enum.GetValues(typeof(Item.Category))) {
List<Item> list = _items[category];
list.Sort();
}
The issue I'm having is that when I call List<Item>.Sort()
, it's obviously not making use of the class-specific CompareTo()
functions. What's the proper way to approach this?