public class Person {
//should be properties, but to be brief we'll use fields
public string Name;
public int Age;
public Person(string name, int age) {
Name = name;
Age = age;
}
}
public class PersonCollection : CollectionBase {
class Strategy : IComparer {
public int Compare(object x, object y) {
Person px = x as Person;
Person py = y as Person;
return String.Compare(px.Name, py.Name);
}
}
public void SortByName() {
this.InnerList.Sort(new Strategy());
}
public void Add(Person value) {
this.InnerList.Add(value);
}
}
class Program
{
static void Main(string[] args) {
PersonCollection people = new PersonCollection();
people.Add(new Person("Bubba", 20));
people.Add(new Person("Al", 22));
people.Add(new Person("Cooter", 99));
people.SortByName();
//prints: Al, Bubba, Cooter
foreach(Person p in people)
Console.WriteLine(p.Name);
}
}