員工類
using System;
namespace int冒泡排序
{
public class Employee
{
private String name;
private int xinShui;
//封裝屬性
public String Name { get; private set; }
public int XinShui { get; private set; }
//構造方法 用來賦值
public Employee(String name,int xinShui) {
this.name = name;
this.xinShui = xinShui;
}
//比較兩個值 大小
public static bool CommonSort(Employee e1,Employee e2)
{
if (e1.xinShui > e2.xinShui) {
return true;
}
return false;
}
//重寫tostirng方法 輸出值
public override string ToString() {
Console.WriteLine(name+"=>"+xinShui);
return "";
}
}
}
//通用排序 方法
//T 任意類型
void CommonSort<T>(T[] array,Func<T,T,bool> EmployeeMethod)
{
//是否循環
bool aa = true;
do
{
aa = false;
for (int i = 0; i < array.Length - 1; i++)
{
//比較 兩個數的值的大小
if (EmployeeMethod(array[i],array[i+1]))
{
//進行交換
T empty = array[i];
array[i] = array[i + 1];
array[i + 1] = empty;
aa = true;
}
}
} while (aa);
//輸出交換後的值
foreach (var item in array)
{
Console.WriteLine(item);
}
}
//測試
Employee[] e1 = new Employee[]{
new Employee("a1",213),
new Employee("sad",231),
new Employee("gg",56),
new Employee("awsdwad",1)
};
//調用通用排序方法
CommonSort<Employee>(e1,Employee.CommonSort);
Console.ReadKey();
輸出結果: