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