usingSystem;usingSystem.Collections;//注意using CollectionsusingSystem.Collections.Generic;usingSystem.Text;namespaceZyyLove2008
{classProgram
{staticvoidMain(string[] args)
{
ArrayList list=newArrayList();
Random random=newRandom();for(inti=1; i<10; i++)
{intindex=random.Next(1,10);
Person person=newPerson("zhang"+index.ToString(), index);
list.Add(person);
}foreach(Person personTmpinlist)
{
Console.WriteLine(personTmp.Name+"\t"+personTmp.Age.ToString());
}
list.Sort(newPersonComparer());
Console.WriteLine("\r\nAfter sorting
\r\n");foreach(Person personTmpinlist)
{
Console.WriteLine(personTmp.Name+"\t"+personTmp.Age.ToString());
}
Console.Read();
}
}publicclassPerson
{privatestring_name;publicstringName
{get{return_name;
}set{
_name=value;
}
}privateint_age;publicintAge
{get{return_age;
}set{
_age=value;
}
}publicPerson(stringname,intage)
{this._name=name;this._age=age;
}
}publicclassPersonComparer : IComparer
{#regionIComparer Members//Compares two objects and returns a value indicating whether one is less than,///equal to, or greater than the other.//The first object to compare.///The second object to compare.///publicintCompare(objectx,objecty)
{
Person personX=xasPerson;if(personX==null)
{thrownewArgumentException("x value is not a type of Person","x");
}
Person personY=yasPerson;if(personY==null)
{thrownewArgumentException("y value is not a type of Person","y");
}returnpersonX.Age.CompareTo(personY.Age);
}#endregion}
}