List<T> 排序(Sort)查找(Find)功能的多种实现
记录 List<T>功能中的Sort和Find功能的几种实现方式
Person类
public class Person:IComparable<Person>
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
public int CompareTo(Person p)
{
return this.Age - p.Age;
}
public static int NameCompareTo(Person p1, Person p2)
{
return System.Collections.Comparer.Default.Compare(p1.Name, p2.Name);
}
}
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
public int CompareTo(Person p)
{
return this.Age - p.Age;
}
public static int NameCompareTo(Person p1, Person p2)
{
return System.Collections.Comparer.Default.Compare(p1.Name, p2.Name);
}
}
调用类
List<Person> Persons = new List<Person>() {
new Person("jdmin",20),
new Person("aleax",35),
new Person("xhina",23)
};
//排序
//方法 1. 让Person类继承IComparable接口,实现CompareTo方法
//Persons.Sort();
//方法2.自定义一个方法来实现
Persons.Sort(Person.NameCompareTo);
//Persons.Sort(delegate(Person p1, Person p2) { return Comparer.Default.Compare(p2.Name, p1.Name); });
foreach (Person p in Persons)
{
Console.WriteLine(p.Name + " Age:" + p.Age);
}
Console.WriteLine("------------------------------");
//方法3. 使用Lambada
//"Lambda表达式" 的语法格式:
//参数列 => 语句或语句块
//如果参数列中有0个或1个以上参数,则必须使用括号括住参数列
var temList = Persons.OrderBy(p => p.Name);//名字升序, A-Z
//var temList = Persons.OrderByDescending(p => p.Age);//按年龄降序
foreach (Person p in temList)
{
Console.WriteLine(p.Name + " Age:" + p.Age);
}
Console.WriteLine("------------------------------");
//查找年龄>25的数据
//查找单个数据
Person oPerson = Persons.Find(delegate(Person p) { return p.Name == "aleax"; });
Console.WriteLine("Name: {0}, Age: {1}", oPerson.Name, oPerson.Age);
//查找出满足条件的所有数据
List<Person> ageList = Persons.FindAll(AgeFind);
//var ageList = Persons.FindAll(delegate(Person p) { return p.Age > 25; });
//var ageList = Persons.Where(p => p.Age > 25);
foreach (Person p in ageList)
{
Console.WriteLine(p.Name + " Age:" + p.Age);
}
Console.WriteLine("------------------------------");
//查看指定的名字是否存在
var bUserExists=Persons.Exists(delegate(Person p){return p.Name=="aleax";});
Console.WriteLine(bUserExists);
new Person("jdmin",20),
new Person("aleax",35),
new Person("xhina",23)
};
//排序
//方法 1. 让Person类继承IComparable接口,实现CompareTo方法
//Persons.Sort();
//方法2.自定义一个方法来实现
Persons.Sort(Person.NameCompareTo);
//Persons.Sort(delegate(Person p1, Person p2) { return Comparer.Default.Compare(p2.Name, p1.Name); });
foreach (Person p in Persons)
{
Console.WriteLine(p.Name + " Age:" + p.Age);
}
Console.WriteLine("------------------------------");
//方法3. 使用Lambada
//"Lambda表达式" 的语法格式:
//参数列 => 语句或语句块
//如果参数列中有0个或1个以上参数,则必须使用括号括住参数列
var temList = Persons.OrderBy(p => p.Name);//名字升序, A-Z
//var temList = Persons.OrderByDescending(p => p.Age);//按年龄降序
foreach (Person p in temList)
{
Console.WriteLine(p.Name + " Age:" + p.Age);
}
Console.WriteLine("------------------------------");
//查找年龄>25的数据
//查找单个数据
Person oPerson = Persons.Find(delegate(Person p) { return p.Name == "aleax"; });
Console.WriteLine("Name: {0}, Age: {1}", oPerson.Name, oPerson.Age);
//查找出满足条件的所有数据
List<Person> ageList = Persons.FindAll(AgeFind);
//var ageList = Persons.FindAll(delegate(Person p) { return p.Age > 25; });
//var ageList = Persons.Where(p => p.Age > 25);
foreach (Person p in ageList)
{
Console.WriteLine(p.Name + " Age:" + p.Age);
}
Console.WriteLine("------------------------------");
//查看指定的名字是否存在
var bUserExists=Persons.Exists(delegate(Person p){return p.Name=="aleax";});
Console.WriteLine(bUserExists);