Ilist排序,写一个对象比较的类,实现IComparer接口

61 篇文章 0 订阅

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;
            }

        }

    }


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

     internal   class  ListComparer < TBaseBusinessObject >  : IComparer < TBaseBusinessObject >
    
{
        
private string propertyName;

        
public ListComparer(string PropertyName)
        
{
            propertyName 
= PropertyName;
        }

        

        
#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

    }

然后是写了一个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();
        }

    }


于是就写完了,用的时候

    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 );

完了~~~

也可以参考:http://blog.csdn.net/rye1123/archive/2007/11/01/1861699.aspx

写个方法,每次调用一下下!!!!!!

也可以参考,http://blog.csdn.net/fan158/archive/2009/08/04/4407170.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值