C# 用实例来理解IComparable和IComparer

时间:2021-11-27 01:41:44

通过Array的Sort方法来理解的 Sort方法要 通过对象去继承IComparable接口来实现排序(当然也有其它办法),我想入门这可能就是对这句话有点不理解,在下面会有注释

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
/// <summary>
/// 设置FirstName和LastName两个属性为排序条件,对person安姓名进行排序
/// </summary>
public class Person : IComparable
{
private string _firstName;
/// <summary>
/// 第一个字母
/// </summary>
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
/// <summary>
/// 第二个字母
/// </summary>
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public Person(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
//理解接口的作用,在这里 继承了IComparable接口,使得Person类必须实现这个接口(实现接口 就是 实现CompareTo方法,Array.Sort排序的时候 只认CompareTo方法,如果没有继承接口,就算有这个方法 也是会抛出异常)
//实现接口后在Array中 能通过这个被实现的接口方法(规定好了是CompareTo方法),根据返回值 而去处理sort方法(在Array.Sort 中已经规定了ComapreTo方法 返回值代表的意义),这样在Person中就可以 由用户
//自己在CompareTo方法中随便的去定义 方法的实现方式,只要返回值满足 规定 即可,真是 妙极了(0相等1大于-1小于)
public int CompareTo(object obj)
{
Person other = obj as Person;
//先比较LastName,如果是0则继续比较FirstName
int result = this.LastName.CompareTo(other.LastName);
if (result == 0)
{
result = this.FirstName.CompareTo(other.FirstName);
}
return result;
}
public override string ToString()
{
return FirstName + " " + LastName;
}
}


class Program
{
static void Main(string[] args)
{
Person[] persons = { new Person("z","l"),
new Person("w","x"),
new Person("l","s"),
new Person("l","m"),
new Person("a","l")};
Array.Sort(persons);//排序
foreach (Person p in persons)
{
Console.WriteLine(p);
}

}
}
}

上面的情况是 可以对 Person类进行修改的情况下的做法,如果不能修改Person呢,这个时候就要IComparer接口大显身手了

利用了Sort方法的重载,添加一个对IComparer接口的实现的接口即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
/// <summary>
/// 设置FirstName和LastName两个属性为排序条件,对person安姓名进行排序
/// </summary>
public class Person //: IComparable
{
private string _firstName;
/// <summary>
/// 第一个字母
/// </summary>
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
/// <summary>
/// 第二个字母
/// </summary>
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public Person(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
//理解接口的作用,在这里 继承了IComparable接口,使得Person类必须实现这个接口(实现接口 就是 实现CompareTo方法,Array.Sort排序的时候 只认CompareTo方法,如果没有继承接口,就算有这个方法 也是会抛出异常)
//实现接口后在Array中 能通过这个被实现的接口方法(规定好了是CompareTo方法),根据返回值 而去处理sort方法(在Array.Sort 中已经规定了ComapreTo方法 返回值代表的意义),这样在Person中就可以 由用户
//自己在CompareTo方法中随便的去定义 方法的实现方式,,只要返回值满足 规定 即可,真是 妙极了(0相等1大于-1小于)
public int CompareTo(object obj)
{
Person other = obj as Person;
//先比较LastName,如果是0则继续比较FirstName
int result = this.LastName.CompareTo(other.LastName);
if (result == 0)
{
result = this.FirstName.CompareTo(other.FirstName);
}
return result;
}
public override string ToString()
{
return FirstName + " " + LastName;
}
}
/// <summary>
/// 架设Person是不允许我们去修改的,这个时候我们就用IComparer接口,然后Sort根据被实现的Compaer方法去排序
/// </summary>
public class PersonComparer : IComparer
{
/// <summary>
/// 枚举,排序条件
/// </summary>
public enum PersonCompareType
{
/// <summary>
/// 第一个名字
/// </summary>
FirstName,
LastName
}
private PersonCompareType compareType;
public PersonComparer(PersonCompareType compareType)
{
this.compareType = compareType;
}
public int Compare(object x, object y)
{
Person p1 = x as Person;
Person p2 = y as Person;
switch (compareType)
{
case PersonCompareType.FirstName:
return p1.FirstName.CompareTo(p2.FirstName);

case PersonCompareType.LastName:
return p1.LastName.CompareTo(p2.LastName);

default:
throw new ArgumentException("必须指定比较类型");
}

}
}


class Program
{
static void Main(string[] args)
{
Person[] persons = { new Person("z","l"),
new Person("w","x"),
new Person("l","s"),
new Person("l","m"),
new Person("a","l")};
Array.Sort(persons);//排序
foreach (Person p in persons)
{
Console.WriteLine(p);
}
Console.WriteLine("-----------------");
//注意这个地方的方法重载
Array.Sort(persons, new PersonComparer(PersonComparer.PersonCompareType.FirstName));
foreach (Person p in persons)
{
Console.WriteLine(p);
}
Console.WriteLine("-----------------");
Array.Sort(persons, new PersonComparer(PersonComparer.PersonCompareType.LastName));
foreach (Person p in persons)
{
Console.WriteLine(p);
}

}
}
}