IComparable和IComparer
class skill:IComparer
{
public int ID
{
get;
set;
}
public skill(int _id)
{
this.ID = _id;
}
public int CompareTo(object obj)
{
skill s = (skill)obj;
if (this.ID < s.ID)
{
return -1;
}
else if (this.ID > s.ID)
{
return 1;
}
else
{
return 0;
}
// throw new NotImplementedException();
}
}
IComparable需要实现 public int CompareTo(object obj)方法
class skill:IComparer
{
public int ID
{
get;
set;
}
public skill(int _id)
{
this.ID = _id;
}
public int Compare(object x, object y)
{
skill a = x as skill;
skill b = y as skill;
if (a.ID > b.ID)
{
return 1;
}
else if (a.ID < b.ID)
{
return -1;
}
else
{
return 0;
}
// throw new NotImplementedException();
}
}
IComparer需要实现 public int Compare(object x, object y)方法
class skillCompare : IComparer
{
public int Compare(skill x, skill y)
{
if (x.ID > y.ID)
{
return 1;
}
else if (x.ID < y.ID)
{
return -1;
}
else
{
return 0;
}
}
}
class skill
{
public int ID
{
get;
set;
}
public skill(int _id)
{
this.ID = _id;
}
}
如果使用IComparer 需要写一个类去实现这个接口
调用时用 skillLists.Sort(new skillCompare());