泛型的排序有3种方法,分别是:
1、List<T>.Sort(),只能在集合元素实现了IComparable泛型接口时使用
2、List<T>.Sort(Comparison<T>),Comparison<T>是一个方法委托,它带有2个参数T,返回int类型,可以灵活指定如何排序,但是需要编码时手动指定如何排序;
3、List<T>.Sort(IComparer<T>),使用实现了IComparer<T>接口的类给集合排序,可以灵活指定如何排序,但是需预先定义好类的排序方法
这里介绍第三种方法:
首先定义一个类,用作集合的元素
using System;
using System.Collections.Generic;
/// <summary>
/// 学生类
/// </summary>
public class Student
{
private string name;
// 姓名
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
// 年龄
public int Age
{
get { return age; }
set { age = value; }
}
private string grade;
// 年级
public string Grade
{
get { return grade; }
set { grade = value; }
}
//构造函数
public Student(string name, int age, string grade)
{
this.name = name;
this.age = age;
this.grade = grade;
}
public override string ToString()
{
return this.name + "," + this.age.ToString() + "," + this.grade;
}
}
接着定义一个用于比较的类,实现IComparer<T>泛型接口:
public class StudentComparer : IComparer<Student>
{
public enum CompareType
{
Name,
Age,
Grade
}
private CompareType type;
// 构造函数,根据type的值,判断按哪个字段排序
public StudentComparer(CompareType type)
{
this.type = type;
}
#region IComparer<Student> 成员
public int Compare(Student x, Student y)
{
switch(this.type)
{
case CompareType.Name:
return x.Name.CompareTo(y.Name);
case CompareType.Age:
return x.Age.CompareTo(y.Age);
default://case CompareType.Grade:
return x.Grade.CompareTo(y.Grade);
}
}
#endregion
}
下面就开始排序了:
using System;
using System.Collections.Generic;
public class test
{
public static void Main()
{
List<Student> arr = new List<Student>();
arr.Add(new Student("张三", 7, "一年级"));
arr.Add(new Student("李四", 11, "二年级"));
arr.Add(new Student("王五", 21, "一年级"));
arr.Add(new Student("陈六", 8, "三年级"));
arr.Add(new Student("刘七", 15, "二年级"));
// 调用Sort方法,实现按年级排序
arr.Sort(new StudentComparer(StudentComparer.CompareType.Grade));
// 循环显示集合里的元素
foreach( Student item in arr)
Console.WriteLine(item.ToString());
// 调用Sort方法,实现按姓名排序
arr.Sort(new StudentComparer(StudentComparer.CompareType.Name));
// 循环显示集合里的元素
foreach( Student item in arr)
Console.WriteLine(item.ToString());
}
}
文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/200864/121436.html
1、List<T>.Sort(),只能在集合元素实现了IComparable泛型接口时使用
2、List<T>.Sort(Comparison<T>),Comparison<T>是一个方法委托,它带有2个参数T,返回int类型,可以灵活指定如何排序,但是需要编码时手动指定如何排序;
3、List<T>.Sort(IComparer<T>),使用实现了IComparer<T>接口的类给集合排序,可以灵活指定如何排序,但是需预先定义好类的排序方法
这里介绍第三种方法:
首先定义一个类,用作集合的元素
using System;
using System.Collections.Generic;
/// <summary>
/// 学生类
/// </summary>
public class Student
{
private string name;
// 姓名
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
// 年龄
public int Age
{
get { return age; }
set { age = value; }
}
private string grade;
// 年级
public string Grade
{
get { return grade; }
set { grade = value; }
}
//构造函数
public Student(string name, int age, string grade)
{
this.name = name;
this.age = age;
this.grade = grade;
}
public override string ToString()
{
return this.name + "," + this.age.ToString() + "," + this.grade;
}
}
接着定义一个用于比较的类,实现IComparer<T>泛型接口:
public class StudentComparer : IComparer<Student>
{
public enum CompareType
{
Name,
Age,
Grade
}
private CompareType type;
// 构造函数,根据type的值,判断按哪个字段排序
public StudentComparer(CompareType type)
{
this.type = type;
}
#region IComparer<Student> 成员
public int Compare(Student x, Student y)
{
switch(this.type)
{
case CompareType.Name:
return x.Name.CompareTo(y.Name);
case CompareType.Age:
return x.Age.CompareTo(y.Age);
default://case CompareType.Grade:
return x.Grade.CompareTo(y.Grade);
}
}
#endregion
}
下面就开始排序了:
using System;
using System.Collections.Generic;
public class test
{
public static void Main()
{
List<Student> arr = new List<Student>();
arr.Add(new Student("张三", 7, "一年级"));
arr.Add(new Student("李四", 11, "二年级"));
arr.Add(new Student("王五", 21, "一年级"));
arr.Add(new Student("陈六", 8, "三年级"));
arr.Add(new Student("刘七", 15, "二年级"));
// 调用Sort方法,实现按年级排序
arr.Sort(new StudentComparer(StudentComparer.CompareType.Grade));
// 循环显示集合里的元素
foreach( Student item in arr)
Console.WriteLine(item.ToString());
// 调用Sort方法,实现按姓名排序
arr.Sort(new StudentComparer(StudentComparer.CompareType.Name));
// 循环显示集合里的元素
foreach( Student item in arr)
Console.WriteLine(item.ToString());
}
}
文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/200864/121436.html