委托员工冒泡排序

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);
    }
    
}

实现截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值