对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口
2012-02-03 00:40
676 查看
1:比较和排序的概念
比较:两个实体类之间按>,=,<进行比较。
排序:在集合类中,对集合类中的实体进行排序。排序基于的算法基于实体类提供的比较函数。
基本型别都提供了默认的比较算法,如string提供了按字母进行比较,int提供了按整数大小进行比较。
2:IComparable和IComparer
这两个接口上一日记已经介绍过,现在用一实例再次讲解一次
当我们创建了自己的实体类,如Student,默认想要对其按照年龄进行排序,则需要为实体类实现IComparable接口。
class Student:IComparable
{
public string Name { get; set; }
public int Age { get; set; }
#region IComparable Members
public int CompareTo(object obj)
{
Student student = obj as Student;
if (Age > student.Age)
{
return 1;
}
else if (Age == student.Age)
{
return 0;
}
else
{
return -1;
}
//return Age.CompareTo(student.Age);
}
#endregion
}
运行结果:
a1----1
f1----2
b1----4
g1----5
OK,疑问来了。如果不想使用年龄作为比较器了,那怎么办。这个时候IComparer的作用就来了,可使用IComparer来实现一个自定义的比较器。如下:
这个时候,我们在排序的使用为Sort方法提供此比较器:
studentList.Sort(SortName.Default);
运行的结果是:
a1----1
b1----4
f1----2
g1----5
上一篇日记已经说过,
一般情况下,我们使用 IComparable 给出类的默认比较代码,使用其他类给出非默认的比较代码。
上面的示例,其中,我们在Student给出了默认的比较代码(实现了IComparable),所以我们可以通过 .Sort()来进行排序。
同时我们也可以给出非默认的比较代码,比如示例中的SortName (要求实现 IComparer接口),所以我们可以通过 .Sort(SortName.Default)来进行排序。
只要是实现是 IComparer接口就可以作为比较器参数丢给Sort().
3:IComparable和IComparer的泛型实现IComparable<T>和IComparer<T>
如果我们稍有经验,我们就会发现上面的代码我们使用了一个已经不建议使用的集合类ArrayList。当泛型出来后,所有非泛型集合类已经建议不尽量使用了。至于原因,从上面的代码中我们也可以看出一点端倪。
注意查看这个Compare函数,如:
我们发现这个函数进行了装箱和拆箱。而这是会影响性能的。如果我们的集合中有成千上万个复杂的实体对象,则在排序的时候所耗费掉的性能就是客观的。而泛型的出现,就可以避免掉拆箱和装箱。
故上文代码中的ArrayList,应该换成List<T>,对应的,我们就该实现IComparable<T>和IComparer<T>。最终的代码应该像:
通过上面的示例,我们可以知道,实现泛型接口,可以使代码更为简洁。
比较:两个实体类之间按>,=,<进行比较。
排序:在集合类中,对集合类中的实体进行排序。排序基于的算法基于实体类提供的比较函数。
基本型别都提供了默认的比较算法,如string提供了按字母进行比较,int提供了按整数大小进行比较。
2:IComparable和IComparer
这两个接口上一日记已经介绍过,现在用一实例再次讲解一次
当我们创建了自己的实体类,如Student,默认想要对其按照年龄进行排序,则需要为实体类实现IComparable接口。
class Student:IComparable
{
public string Name { get; set; }
public int Age { get; set; }
#region IComparable Members
public int CompareTo(object obj)
{
Student student = obj as Student;
if (Age > student.Age)
{
return 1;
}
else if (Age == student.Age)
{
return 0;
}
else
{
return -1;
}
//return Age.CompareTo(student.Age);
}
#endregion
}
PS:注意上面代码中CompareTo方法有一条注释的代码,其实本函数完全可以使用该注释代码代替,因为利用了整形的默认比较方法。此处未使用本注释代码,是为了更好的说明比较器的工作原理。
接下来写一个测试用例:
public Form1() { InitializeComponent(); studentList = new ArrayList(); studentList.Add(new Student() { Age = 1, Name = "a1" }); studentList.Add(new Student() { Age = 5, Name = "g1" }); studentList.Add(new Student() { Age = 4, Name = "b1" }); studentList.Add(new Student() { Age = 2, Name = "f1" }); } ArrayList studentList; private void button1_Click(object sender, EventArgs e) { studentList.Sort(); foreach (Student item in studentList) { this.textBox1.Text += item.Name + "----" +item.Age.ToString() + "\r\n" ; } }
运行结果:
a1----1
f1----2
b1----4
g1----5
OK,疑问来了。如果不想使用年龄作为比较器了,那怎么办。这个时候IComparer的作用就来了,可使用IComparer来实现一个自定义的比较器。如下:
class SortName: IComparer {
public static IComparer Default = new PersonComparerName();
#region IComparer Members public int Compare(object x, object y) { Student s1 = x as Student; Student s2 = y as Student; return s1.Name.CompareTo(s2.Name); } #endregion }
这个时候,我们在排序的使用为Sort方法提供此比较器:
studentList.Sort(SortName.Default);
运行的结果是:
a1----1
b1----4
f1----2
g1----5
上一篇日记已经说过,
一般情况下,我们使用 IComparable 给出类的默认比较代码,使用其他类给出非默认的比较代码。
上面的示例,其中,我们在Student给出了默认的比较代码(实现了IComparable),所以我们可以通过 .Sort()来进行排序。
同时我们也可以给出非默认的比较代码,比如示例中的SortName (要求实现 IComparer接口),所以我们可以通过 .Sort(SortName.Default)来进行排序。
只要是实现是 IComparer接口就可以作为比较器参数丢给Sort().
3:IComparable和IComparer的泛型实现IComparable<T>和IComparer<T>
如果我们稍有经验,我们就会发现上面的代码我们使用了一个已经不建议使用的集合类ArrayList。当泛型出来后,所有非泛型集合类已经建议不尽量使用了。至于原因,从上面的代码中我们也可以看出一点端倪。
注意查看这个Compare函数,如:
public int Compare(object x, object y) { Student s1 = x as Student; Student s2 = y as Student; return s1.Name.CompareTo(s2.Name); }
我们发现这个函数进行了装箱和拆箱。而这是会影响性能的。如果我们的集合中有成千上万个复杂的实体对象,则在排序的时候所耗费掉的性能就是客观的。而泛型的出现,就可以避免掉拆箱和装箱。
故上文代码中的ArrayList,应该换成List<T>,对应的,我们就该实现IComparable<T>和IComparer<T>。最终的代码应该像:
public partial class Form1 : Form { public Form1() { InitializeComponent(); studentList = new List<Student>(); studentList.Add(new Student() { Age = 1, Name = "a1" }); studentList.Add(new Student() { Age = 5, Name = "g1" }); studentList.Add(new Student() { Age = 4, Name = "b1" }); studentList.Add(new Student() { Age = 2, Name = "f1" }); } List<Student> studentList; private void button1_Click(object sender, EventArgs e) { studentList.Sort(new SortName()); foreach (Student item in studentList) { this.textBox1.Text += item.Name + "----" +item.Age.ToString() + "\r\n" ; } } } class Student:IComparable<Student> { public string Name { get; set; } public int Age { get; set; } #region IComparable<Student> Members public int CompareTo(Student other) { return Age.CompareTo(other.Age); } #endregion } class SortName: IComparer<Student> { #region IComparer<Student> Members public int Compare(Student x, Student y) { return x.Name.CompareTo(y.Name); } #endregion }
通过上面的示例,我们可以知道,实现泛型接口,可以使代码更为简洁。
相关文章推荐
- 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口
- 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口(转)
- 用IComparable和IComparable<T>接口实现两个类对象的比较大小.
- Effective C# 学习笔记(三十一)利用IComparable<T>和IComparer<T>接口来实现排序关系
- Java编程之TreeSet排序两种解决方法(1)元素自身具备比较功能,元素需要实现Comparable接口覆盖compare(2)创建根据自定义Person类的name进行排序的Comparator
- 比较器的使用,实现 Comparable接口,功能:实现对象的多条件比较,排序
- 对象的比较与排序(七):对泛型列表进行排序和搜索:Comparison<T>和Predicate<T>的应用
- 实现比较器接口IComparable<T>,让自定义类型数组也能排序
- Java中实现对象的比较:Comparable接口和Comparator接口
- 使用 Comparable 接口实现对象的属性排序
- Java中实现对象的比较:Comparable接口和Comparator接口
- 实现Comparable接口对存放自定义对象列表(数组)进行排序
- Java中实现对象的比较:Comparable接口和Comparator接口
- 用泛型的IEqualityComparer<T>接口去重复项
- 数组排序方法的性能比较(2):Array.Sort<T>实现分析
- Java实例对象间的比较(实现Comparable接口)
- 利用反射实现mysql数据库sql查询 返回List<E>泛型(持久化)对象
- Java中实现对象的比较:Comparable接口和Comparator接口
- C# List<> 实现 IComparer 接口 排序
- 一起谈.NET技术,数组排序方法的性能比较(中):Array.Sort<T> 实现分析