自定义List<>类按照指定属性排序
本文关键字: c#, List, 排序实现接口并重写实现 CompareTo方法(可按需编辑需要排序的属性先后属性,示例是按照cout后按照num)
public class Yilou : IComparable<Yilou>
{
public int num;
public int count;
#region IComparable<Yilou> 成员
int IComparable<Yilou>.CompareTo(Yilou other)
{
if (other == null)
return 1;
int value = this.count – other.count;
if (value == 0)
value = this.num – other.num;
return value;
}#endregion
}
示例List:直接调用 yilouList.Sort();即可实现按Yilou类的count对Yilou排序