ICompareable和IComparer结合 比较大小
将ICompareable和IComparer结合 比较大小
这儿注意的是PersonComparer的比较方法 CompareTo调用了Person对象的重载比较方法CompareTO
public int CompareTo(object obj, PersonCompareType compareType)
{.....
}
这样的话,Person能根据Comparer传过来的参数,进行相应的排序比较
using System;
using System.Collections;//注意using Collections
using System.Collections.Generic;
using System.Text;
namespace ZyyLove2008
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Random random = new Random();
list.Add(new Person("A", 4));
list.Add(new Person("B", 3));
list.Add(new Person("D", 5));
list.Add(new Person("C", 2));
foreach (Person personTmp in list)
{
Console.WriteLine(personTmp.Name + "\t" + personTmp.Age.ToString());
}
list.Sort(new PersonComparer(PersonCompareType.Name));
Console.WriteLine("\r\nAfter sorting by Name\r\n");
foreach (Person personTmp in list)
{
Console.WriteLine(personTmp.Name + "\t" + personTmp.Age.ToString());
}
list.Sort(new PersonComparer(PersonCompareType.Age));
Console.WriteLine("\r\nAfter sorting by Age\r\n");
foreach (Person personTmp in list)
{
Console.WriteLine(personTmp.Name + "\t" + personTmp.Age.ToString());
}
Console.Read();
}
}
public class Person : IComparable
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private int _age;
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public Person(string name, int age)
{
this._name = name;
this._age = age;
}
#region IComparable Members
public int CompareTo(object obj)
{
Person personTmp = obj as Person;
if (personTmp == null)
{
throw new ArgumentException("obj value is not a type of Person", "obj");
}
return string.Compare(this.Name, personTmp.Name);
}
/// <summary>
/// Person's Overloaded CompareTo Method
/// </summary>
/// <param name="obj"></param>
/// <param name="compareType"></param>
/// <returns></returns>
public int CompareTo(object obj, PersonCompareType compareType)
{
int result = 0;
Person personTmp = obj as Person;
if (personTmp == null)
{
throw new ArgumentException("obj value is not a type of Person", "obj");
}
switch (compareType)
{
case PersonCompareType.Default:
case PersonCompareType.Name:
result= string.Compare(this.Name, personTmp.Name);
break;
case PersonCompareType.Age:
result= this.Age.CompareTo(personTmp.Age);
break;
default:
System.Diagnostics.Debug.Assert(false);
break;
}
return result;
}
#endregion
}
public class PersonComparer : IComparer
{
private PersonCompareType _compareType;
public PersonCompareType CompareType
{
get
{
return _compareType;
}
set
{
_compareType = value;
}
}
public PersonComparer(PersonCompareType compareType)
{
_compareType = compareType;
}
#region IComparer Members
/// <summary>
/// Compares two objects and returns a value indicating whether one is less than,
/// equal to, or greater than the other.
/// </summary>
/// <param name="x">The first object to compare.</param>
/// <param name="y">The second object to compare.</param>
/// <returns></returns>
public int Compare(object x, object y)
{
Person personX = x as Person;
if (personX == null)
{
throw new ArgumentException("x value is not a type of Person", "x");
}
Person personY = y as Person;
if (personY == null)
{
throw new ArgumentException("y value is not a type of Person", "y");
}
return personX.CompareTo(personY, _compareType);
}
#endregion
}
public enum PersonCompareType
{
Default,
Name,
Age
}
}