using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//http://www.cnblogs.com/jiahaipeng/archive/2008/10/23/1317430.html 此文的演示代码。
namespace DelegatePrograme
{
delegate bool CompareOp(object lhs, object rhs);
class Program
{
static void Main(string[] args)
{
Employee[] employees =
{
new Employee("Jone",2000),
new Employee("Tom",2500),
new Employee("Buddy",2300),
new Employee("Wiley",2800),
new Employee("Fudd",1900),
};
CompareOp compareOp = new CompareOp(Employee.Compare);
BubbleSorter.Sort(employees, compareOp);
for (int i = 0; i < employees.Length; i++)
Console.WriteLine(employees[i].ToString());
Console.ReadLine();
}
}
class BubbleSorter
{
/**/
/// <summary>
/// 冒泡排序方法
/// </summary>
/// <param name="sortArray">要排序的一组对象</param>
/// <param name="getCompareMethod">判断两个对象大小的方法</param>
public static void Sort(object[] sortArray, CompareOp getCompareMethod)
{
for (int i = 0; i < sortArray.Length; i++)
for (int j = i + 1; j < sortArray.Length; j++)
if (getCompareMethod(sortArray[i], sortArray[j]))
{
object temp = sortArray[i];
sortArray[i] = sortArray[j];
sortArray[j] = temp;
}
}
}
class Employee
{
private string _Name;
private decimal _Salary;
/**/
/// <summary>
/// 取得雇员的姓名
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/**/
/// <summary>
/// 取得雇员的工资
/// </summary>
public decimal Salary
{
get { return _Salary; }
set { _Salary = value; }
}
/**/
/// <summary>
/// 构造方法
/// </summary>
/// <param name="name"></param>
/// <param name="salary"></param>
public Employee(string name, decimal salary)
{
this._Salary = salary;
this._Name = name;
}
/**/
/// <summary>
/// 重写了tostring()方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format(_Name + ",{0:C}", _Salary);
}
/**/
/// <summary>
/// 比较两个对象的大小
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static bool Compare(object lhs, object rhs)
{
Employee empLhs = (Employee)lhs;
Employee empRhs = (Employee)rhs;
return (empLhs._Salary > empRhs._Salary) ? true : false;
}
}
}