using System;
using System.Collections;
class ArrayListTest
{
public static void Main()
{
ArrayList personList =
new
ArrayList();
personList.Add(
new
Person(
"Barbara"
,
"Denver"
, 34));
personList.Add(
new
Person(
"Sofia"
,
"Brucks"
, 32));
personList.Add(
new
Person(
"Elena"
,
"Silver"
, 31));
personList.Add(
new
Person(
"Asia"
,
"Solis"
, 38));
personList.Add(
new
Person(
"Paris"
,
"Jordan"
, 25));
Console.WriteLine(
"List of Person (not sorted)"
);
foreach(Person p
in
personList)
{
Console.WriteLine(p.ToString());
}
personList.Sort();
Console.WriteLine();
Console.WriteLine(
"List of Person (sorted)"
);
foreach(Person p
in
personList)
{
Console.WriteLine(p.ToString());
}
}
}
class Person : IComparable
{
string name;
string surname;
byte age;
public Person()
{}
public Person(string n, string s, byte a)
{
name = n;
surname = s;
age = a;
}
public override string ToString()
{
return
name+
", "
+ surname +
", ("
+ age +
")"
;
}
public int CompareTo(object obj)
{
if
(obj is Person) {
Person p = (Person) obj;
return
age.CompareTo(p.age);
}
throw
new
ArgumentException(
"object is not a Person"
);
}
}