public class Person : IComparableIn this code example, we are creating a class called Person which implements the IComparable interface. We define a CompareTo method for comparing two Person objects by their Age property. We then create a list of Person objects and sort them using the Sort method inherited from the List{ public string Name { get; set; } public int Age { get; set; } public int CompareTo(Person other) { // Compare by age return this.Age.CompareTo(other.Age); } } List people = new List { new Person { Name = "Alex", Age = 30 }, new Person { Name = "Beth", Age = 25 }, new Person { Name = "Chris", Age = 35 }, new Person { Name = "David", Age = 20 } }; people.Sort(); foreach (Person p in people) { Console.WriteLine($"{p.Name} ({p.Age})"); }