3.泛型排序
List<T>类可以对其元素排序.Sort()方法有几个重载的版本:
public void List<T>.Sort();
public void List<T>.Sort(Comparison<T>);
public void List<T>.Sort(IComparer<T>);
public void List<T>.Sort(Int32,Int32,IComparer<T>);
无参数的只能在集合中的元素实现了IComparable接口是使用.
Comparison<T>是一个委托,带有两个类型的参数,返回Int,如果参数相等,该方法就返回零,如果第一个小于第二个参数,该方法就返回一个小于0 的值,否则相反.
比较方法可以使用匿名方法,两个参数类型是Racer,在代码中,使用字符串CompareTo()比较Name属性:
racer.Sort(
delegate
(Racer r1, Racer r2)
{ return r1.Name.CompareTo(r2.Name); }
);
除了使用委托外,ICompare<T>还用于给集合排序.RacerComparer类为Racer类实现了Icompare<T>接口,这个类可以按照名字,和汽车排序:
public
class
RacerComparer : IComparer
<
Racer
>
{
public enum CompareType
{
Name,
Car
}
private CompareType compareType;
public RacerComparer(CompareType compareType)
{
this.compareType = compareType;
}
public int Compare(Racer x, Racer y)
{
int result = 0;
switch (compareType)
{
case CompareType.Name:
result=x.Name.CompareTo(y.Name);
break;
case CompareType.Car:
result=x.Car.CompareTo(y.Car);
break;
}
return result;
}
}
截止到这里
{
public enum CompareType
{
Name,
Car
}
private CompareType compareType;
public RacerComparer(CompareType compareType)
{
this.compareType = compareType;
}
public int Compare(Racer x, Racer y)
{
int result = 0;
switch (compareType)
{
case CompareType.Name:
result=x.Name.CompareTo(y.Name);
break;
case CompareType.Car:
result=x.Car.CompareTo(y.Car);
break;
}
return result;
}
}
我吧全部代码发上来
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace ListTest
{
class Program
{
static void Main(string[] args)
{
List<Racer> racer = new List<Racer>();
//添加
racer.Add(new Racer("张三", "奇瑞"));
racer.Add(new Racer("李四", "中华"));
racer.Add(new Racer("王五", "神牛"));
racer.Add(new Racer("王六", "神牛"));
foreach (Racer r in racer)
{
Console.WriteLine(r);
}
Console.WriteLine("****************查找********************");
FindRacer finder = new FindRacer("神牛");
foreach (Racer race in racer.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))
{
Console.WriteLine(race);
}
Console.WriteLine("****************输出********************");
racer.ForEach(delegate(Racer r) { Console.WriteLine(r); });
Console.WriteLine("****************排序********************");
racer.Sort(delegate(Racer r1, Racer r2) { return r1.Name.CompareTo(r2.Name); });
racer.ForEach(delegate(Racer r) { Console.WriteLine(r); });
Console.WriteLine("****************自定义排序********************");
racer.Sort(new RacerComparer(RacerComparer.CompareType.Car));
racer.ForEach(delegate(Racer r) { Console.WriteLine(r); });
}
}
public class FindRacer
{
private string car;
public FindRacer(string car)
{
this.car = car;
}
public bool DrivingCarPredicate(Racer racer)
{
return racer.Car == car;
}
}
public class Racer
{
private string name;
public string Name
{
get { return name; }
}
private string car;
public String Car
{
get { return car; }
}
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + "," + car;
}
}
public class RacerComparer : IComparer<Racer>
{
public enum CompareType
{
Name,
Car
}
private CompareType compareType;
public RacerComparer(CompareType compareType)
{
this.compareType = compareType;
}
public int Compare(Racer x, Racer y)
{
int result = 0;
switch (compareType)
{
case CompareType.Name:
result=x.Name.CompareTo(y.Name);
break;
case CompareType.Car:
result=x.Car.CompareTo(y.Car);
break;
}
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ListTest
{
class Program
{
static void Main(string[] args)
{
List<Racer> racer = new List<Racer>();
//添加
racer.Add(new Racer("张三", "奇瑞"));
racer.Add(new Racer("李四", "中华"));
racer.Add(new Racer("王五", "神牛"));
racer.Add(new Racer("王六", "神牛"));
foreach (Racer r in racer)
{
Console.WriteLine(r);
}
Console.WriteLine("****************查找********************");
FindRacer finder = new FindRacer("神牛");
foreach (Racer race in racer.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))
{
Console.WriteLine(race);
}
Console.WriteLine("****************输出********************");
racer.ForEach(delegate(Racer r) { Console.WriteLine(r); });
Console.WriteLine("****************排序********************");
racer.Sort(delegate(Racer r1, Racer r2) { return r1.Name.CompareTo(r2.Name); });
racer.ForEach(delegate(Racer r) { Console.WriteLine(r); });
Console.WriteLine("****************自定义排序********************");
racer.Sort(new RacerComparer(RacerComparer.CompareType.Car));
racer.ForEach(delegate(Racer r) { Console.WriteLine(r); });
}
}
public class FindRacer
{
private string car;
public FindRacer(string car)
{
this.car = car;
}
public bool DrivingCarPredicate(Racer racer)
{
return racer.Car == car;
}
}
public class Racer
{
private string name;
public string Name
{
get { return name; }
}
private string car;
public String Car
{
get { return car; }
}
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + "," + car;
}
}
public class RacerComparer : IComparer<Racer>
{
public enum CompareType
{
Name,
Car
}
private CompareType compareType;
public RacerComparer(CompareType compareType)
{
this.compareType = compareType;
}
public int Compare(Racer x, Racer y)
{
int result = 0;
switch (compareType)
{
case CompareType.Name:
result=x.Name.CompareTo(y.Name);
break;
case CompareType.Car:
result=x.Car.CompareTo(y.Car);
break;
}
return result;
}
}
}