Listnumbers = new List { 5, 2, 8, 3, 1 }; numbers.Sort(); // sorts the list in ascending order
class Person : IComparableThe IComparable and Equals interfaces are part of the System namespace, which is a core .NET package library.{ public string Name { get; set; } public int Age { get; set; } public int CompareTo(Person other) { // sort by age in ascending order return Age.CompareTo(other.Age); } public override bool Equals(object obj) { if (obj == null || !(obj is Person)) return false; Person other = (Person)obj; return Name == other.Name && Age == other.Age; } } List people = new List { new Person { Name = "Alice", Age = 28 }, new Person { Name = "Bob", Age = 34 }, new Person { Name = "Charlie", Age = 22 } }; people.Sort(); // sorts the list by age in ascending order