大伟Csharp冒泡排序委托泛型---对类的实例列表进行排序

  1. Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 冒泡排序委托泛型
{
    class Program
    {
        static void Main(string[] args)
        {
            weiSortTUse.weiSortUse01();
            Console.ReadLine();
        }
    }
}

  1. YuanGong.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 冒泡排序委托泛型
{
    class YuanGong
    {
        public string name { get; set; }
        public int age { get; set; }

        public YuanGong(string name,int age)
        {
            this.name = name;
            this.age = age;
        }

        public static bool BiJiao(YuanGong yuanGong1,YuanGong yuanGong2)
        {
            if (yuanGong1.age < yuanGong2.age) return true; //小的排后面去
            return false;
        }

        public override string ToString()
        {
            return name + ":" + age;
        }
    }
}

  1. weiSortT.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 冒泡排序委托泛型
{
    class weiSortT
    {
        public static void WeiSort<T>(List<T> ls,Func<T,T,bool> f1)
        {
            bool youHuanWei = false;
            do
            {
                youHuanWei = false;
                for (int i = 0; i < ls.Count - 1; i++)
                {
                    if (f1(ls[i], ls[i + 1]))
                    {
                        T temp = ls[i];
                        ls[i] = ls[i + 1];
                        ls[i + 1] = temp;
                        youHuanWei = true;
                    }
                }
            } while (youHuanWei);
        }
    }
}

  1. weiSortTUse.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 冒泡排序委托泛型
{
    public class weiSortTUse
    {
        public static void weiSortUse01()
        {
            List<YuanGong> ls = new List<YuanGong>
            {
                new YuanGong("aaa",98),
                new YuanGong("bbb",20),
                new YuanGong("ccc",39),
                new YuanGong("kkk",100),
            };
            weiSortT.WeiSort<YuanGong>(ls, YuanGong.BiJiao);
            foreach (YuanGong yg in ls)
            {
                Console.WriteLine(yg);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值