写了一个对象集合排序的类
废话不多说,首先是定义一个对象实体类
classEntity


{
publicEntity()


{}
privateintid;
publicintId


{
get


{
returnid;
}
set


{
id=value;
}
}
privatestringname;
publicstringName


{
get


{
returnname;
}
set


{
name=value;
}
}

privatedoubleprice;
publicdoublePrice


{
get


{
returnprice;
}
set


{
price=value;
}
}
}

然后写一个对象比较的类,实现IComparer<T>接口。
internalclassListComparer<TBaseBusinessObject>:IComparer<TBaseBusinessObject>


{
privatestringpropertyName;

publicListComparer(stringPropertyName)


{
propertyName=PropertyName;
}



IComparer<tbasebusinessobject></tbasebusinessobject>Members#regionIComparer<TBaseBusinessObject>Members

publicintCompare(TBaseBusinessObjectx,TBaseBusinessObjecty)


{
PropertyInfoproperty=typeof(TBaseBusinessObject).GetProperty(propertyName);
if(property.PropertyType==Type.GetType("System.Int16"))


{
intxNumber=0;
intyNumber=0;
if(property.GetValue(x,null)!=null)


{
xNumber=Convert.ToInt16(property.GetValue(x,null).ToString());
}
if(property.GetValue(y,null)!=null)


{
yNumber=Convert.ToInt16(property.GetValue(y,null).ToString());
}
returnxNumber.CompareTo(yNumber);
}
if(property.PropertyType==Type.GetType("System.Int32"))


{
intxNumber=0;
intyNumber=0;
if(property.GetValue(x,null)!=null)


{
xNumber=Convert.ToInt32(property.GetValue(x,null).ToString());
}
if(property.GetValue(y,null)!=null)


{
yNumber=Convert.ToInt32(property.GetValue(y,null).ToString());
}
returnxNumber.CompareTo(yNumber);
}
if(property.PropertyType==Type.GetType("System.Double"))


{
doublexNumber=0;
doubleyNumber=0;
if(property.GetValue(x,null)!=null)


{
xNumber=Convert.ToDouble(property.GetValue(x,null).ToString());
}
if(property.GetValue(y,null)!=null)


{
yNumber=Convert.ToDouble(property.GetValue(y,null).ToString());
}
returnxNumber.CompareTo(yNumber);
}
if(property.PropertyType==Type.GetType("System.DateTime"))


{
DateTimexTime=DateTime.Now;
DateTimeyTime=DateTime.Now;
if(property.GetValue(x,null)!=null)


{
xTime=Convert.ToDateTime(property.GetValue(x,null).ToString());
}
if(property.GetValue(y,null)!=null)


{
yTime=Convert.ToDateTime(property.GetValue(y,null).ToString());
}
returnxTime.CompareTo(yTime);
}
if((property.PropertyType==Type.GetType("System.String"))||(property.PropertyType==Type.GetType("System.Boolean")))


{
stringxText=string.Empty;
stringyText=string.Empty;
if(property.GetValue(x,null)!=null)


{
xText=property.GetValue(x,null).ToString();
}
if(property.GetValue(y,null)!=null)


{
yText=property.GetValue(y,null).ToString();
}
returnxText.CompareTo(yText);
}
return0;

}

#endregion
}
然后是写了一个List继承List<T>类的集合,
publicclassBaseBusinessObjectList<TBaseBusinessObject>:List<TBaseBusinessObject>


{
publicvoidSort(stringsortfield,boolisAscending)


{
//这里实例化了刚才写的IComparer类。
ListComparer<TBaseBusinessObject>listComparer=newListComparer<TBaseBusinessObject>(sortfield);
base.Sort(listComparer);
if(!isAscending)base.Reverse();
}
}

于是就写完了,用的时候
BaseBusinessObjectList<Entity>list=newBaseBusinessObjectList<Entity>();
Entityobj=newEntity();
obj.Id=1;
obj.Name="test";
obj.Price=3.23;
list.Add(obj);

//按照Name字段向上排序。
list.Sort("Name",true);
//按照Price字段向上排序。
list.Sort("Price",true);
//按照Id字段向下排序。
list.Sort("Id",false);
完了~~~