C#委托之冒泡排序
使用委托实现各种数据排序,此处员工类测试,根据工资对员工排序
Employ.cs
class Employee
{
private string name;
private int salary;
public Employee() { }
public Employee(string name,int salary)
{
this.name = name;
this.salary = salary;
}
public string Name
{
get { return name; }
set { value = name; }
}
public int Salary
{
get { return salary; }
set { value = salary; }
}
public static bool Compare(Employee e1, Employee e2)
{
if (e1.salary>e2.salary)
{
return true;
}
return false;
}
public override string ToString()
{
return string.Format("姓名:"+name+" 工资:"+salary);
}
}
Program.cs
class Program
{
static void Main(string[] args)
{
Employee[] employ = new Employee[]
{
new Employee("zzz",2500),
new Employee("sw",2000),
new Employee("wjh",3000),
new Employee("zly",2800),
new Employee("wzz",1000)
};
ListAction(employ, Employee.Compare);
foreach (var i in employ)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
static void ListAction<T>(T[] arg,Func<T,T,bool> campare) {
bool isTrans = true;
do
{
isTrans = false;
for (int i = 0; i < arg.Length-1; i++)
{
if (campare(arg[i],arg[i+1]))
{
T temp = arg[i];
arg[i] = arg[i + 1];
arg[i + 1] = temp;
isTrans = true;
}
}
} while (isTrans==true);
}
}
实现截图: