一个对象集合排序的类
来源:互联网 发布:域名去哪里注册 编辑:程序博客网 时间:2024/05/18 00:23
写了一个对象集合排序的类
废话不多说,首先是定义一个对象实体类
class Entity
{
public Entity()
{}
private int id;
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
private double price;
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
}
{
public Entity()
{}
private int id;
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
private double price;
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
}
然后写一个对象比较的类,实现IComparer<T>接口。
internal class ListComparer<TBaseBusinessObject> : IComparer<TBaseBusinessObject>
{
private string propertyName;
public ListComparer(string PropertyName)
{
propertyName = PropertyName;
}
IComparer Members#region IComparer<TBaseBusinessObject> Members
public int Compare(TBaseBusinessObject x, TBaseBusinessObject y)
{
PropertyInfo property = typeof(TBaseBusinessObject).GetProperty(propertyName);
if (property.PropertyType == Type.GetType("System.Int16"))
{
int xNumber = 0;
int yNumber = 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());
}
return xNumber.CompareTo(yNumber);
}
if (property.PropertyType == Type.GetType("System.Int32"))
{
int xNumber = 0;
int yNumber = 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());
}
return xNumber.CompareTo(yNumber);
}
if (property.PropertyType == Type.GetType("System.Double"))
{
double xNumber = 0;
double yNumber = 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());
}
return xNumber.CompareTo(yNumber);
}
if (property.PropertyType == Type.GetType("System.DateTime"))
{
DateTime xTime = DateTime.Now;
DateTime yTime = 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());
}
return xTime.CompareTo(yTime);
}
if ((property.PropertyType == Type.GetType("System.String")) || (property.PropertyType == Type.GetType("System.Boolean")))
{
string xText = string.Empty;
string yText = 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();
}
return xText.CompareTo(yText);
}
return 0;
}
#endregion
}
{
private string propertyName;
public ListComparer(string PropertyName)
{
propertyName = PropertyName;
}
IComparer
public int Compare(TBaseBusinessObject x, TBaseBusinessObject y)
{
PropertyInfo property = typeof(TBaseBusinessObject).GetProperty(propertyName);
if (property.PropertyType == Type.GetType("System.Int16"))
{
int xNumber = 0;
int yNumber = 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());
}
return xNumber.CompareTo(yNumber);
}
if (property.PropertyType == Type.GetType("System.Int32"))
{
int xNumber = 0;
int yNumber = 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());
}
return xNumber.CompareTo(yNumber);
}
if (property.PropertyType == Type.GetType("System.Double"))
{
double xNumber = 0;
double yNumber = 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());
}
return xNumber.CompareTo(yNumber);
}
if (property.PropertyType == Type.GetType("System.DateTime"))
{
DateTime xTime = DateTime.Now;
DateTime yTime = 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());
}
return xTime.CompareTo(yTime);
}
if ((property.PropertyType == Type.GetType("System.String")) || (property.PropertyType == Type.GetType("System.Boolean")))
{
string xText = string.Empty;
string yText = 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();
}
return xText.CompareTo(yText);
}
return 0;
}
#endregion
}
然后是写了一个List继承List<T>类的集合,
public class BaseBusinessObjectList<TBaseBusinessObject> : List<TBaseBusinessObject>
{
public void Sort(string sortfield, bool isAscending)
{
//这里实例化了刚才写的IComparer类。
ListComparer<TBaseBusinessObject> listComparer = new ListComparer<TBaseBusinessObject>(sortfield);
base.Sort(listComparer);
if (!isAscending) base.Reverse();
}
}
{
public void Sort(string sortfield, bool isAscending)
{
//这里实例化了刚才写的IComparer类。
ListComparer<TBaseBusinessObject> listComparer = new ListComparer<TBaseBusinessObject>(sortfield);
base.Sort(listComparer);
if (!isAscending) base.Reverse();
}
}
于是就写完了,用的时候
BaseBusinessObjectList<Entity> list = new BaseBusinessObjectList<Entity>();
Entity obj = new Entity();
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);
Entity obj = new Entity();
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);
完了~~~
- 一个对象集合排序的类
- 一个对象集合排序的类
- 写一个对象集合排序的类
- 将一个对象集合按照对象的某个字段排序
- 逐步为对象集合构建一个通用的按指定属性排序的方法
- java 中对象集合的公用排序工具类
- 一个数组对象的排序
- 集合框架-ArrayList集合存储自定义对象的排序案例
- Java集合对象排序
- Java集合对象排序
- Java集合对象排序
- Java集合对象排序
- Java集合对象排序
- list 集合对象 排序
- Java集合对象排序
- Java集合对象排序
- 面向对象---集合排序
- Java集合对象排序
- Tab Mix Plus for Firefox
- Asp.net 2.0 C#实现压缩/解压功能
- 将Asp.net 数据导入Excel
- 博客地址收集
- msmq安装问题的解决过程
- 一个对象集合排序的类
- 以数据库为中心的时代已经终结
- VS2005发布、生成网站时如何设置固定的dll文件名?
- 入门教程:ASP设计常见问题及解答精要1
- 不需xp_cmdshell支持在有注入漏洞的SQL服务器上运行CMD命令
- 我的又一次面试经历---难道真的抛弃delphi,要用java了么?
- SQLserver安全设置攻略
- SQL Server数据库六种数据移动方法
- 入门教程:ASP设计常见问题及解答精要2