using System; using System.Collections; class Program { static void Main(string[] args) { ArrayList numbers = new ArrayList() { 5, 7, 2, 9, 1 }; numbers.Sort(); foreach (int num in numbers) { Console.Write(num + " "); } } }
using System; using System.Collections; class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { ArrayList people = new ArrayList() { new Person() {Name="John", Age=25}, new Person() {Name="Sarah", Age=31}, new Person() {Name="Alice", Age=20}, new Person() {Name="Michael", Age=42} }; people.Sort(new PersonAgeComparer()); foreach (Person p in people) { Console.WriteLine(p.Name + ", " + p.Age); } } } class PersonAgeComparer : IComparer { public int Compare(Object x, Object y) { Person px = (Person)x; Person py = (Person)y; return px.Age.CompareTo(py.Age); } }In this example, an ArrayList is created with custom Person objects. The ArrayList is sorted using the Sort() method with a custom comparer that compares the Age property of the Person objects. The output is sorted by age in ascending order. The package library is System.Collections in the .NET framework.