110,502
社区成员
发帖
与我相关
我的任务
分享
如 public class Class1: IComparable<Class1>
{
public string _id = String.Empty;
/// <summary>
///自定义比较器
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(Class1 other)
{
if (other == null)
return 1;
if (this == null)
return -1;
int _idthis=0;
int _idother=0;
if(int.TryParase(this.Id,out _idthis) && int.TryParse(other.Id,out _idother))
{
return _idthis.CompareTo(_idother);
}
else if(int.TryParase(this.Id,out _idthis))
{
return 1;
)
else if(int.TryParase(other.Id,out _idother))
{
return _1;
)
return this.Id.CompareTo(other.Id);
}
如:List<Class1> list=new List<Class1>();
list.Add(new Class1("111"));
list.Add(new Class1("2222"));
list.Add(new Class1("112D1"));
list.Add(new Class1("222C2"));
list.sort(); //进行排序