public class Product : IComparable<Product>
{
    
private int id;
    
private string prodName;
    
private decimal price;

    
public static Comparison<Product> PriceComparison = delegate(Product p1, Product p2)
                                                        {
                                                            
return p1.price.CompareTo(p2.price);
                                                        };

    
public static Comparison<Product> IDComparison = delegate(Product p1, Product p2)
                                                     {
                                                         
return p1.id.CompareTo(p2.id);
                                                     };

    
public int ProductID
    {
        
get { return id; }
        
set { id = value; }
    }

    
public string ProductName
    {
        
get { return prodName; }
        
set { prodName = value; }
    }

    
public decimal UnitPrice
    {
        
get { return price; }
        
set { price = value; }
    }

    
public Product(int id, string prodName, decimal price)
    {
        
this.id = id;
        
this.prodName = prodName;
        
this.price = price;
    }

    
#region IComparable<Product> Members

    
public int CompareTo(Product other)
    {
        
return ProductName.CompareTo(other.ProductName);
    }

    
#endregion

    
public override string ToString()
    {
        
return string.Format("Id: {0} Name: {1} Price: {2}", id, prodName, price);
    }
}


List
<Product> products = new List<Product>();

products.Sort(
delegate(Product p1, Product p2)
              {
                  
return p1.ProductName.CompareTo(p2.ProductName);
              });
posted on 2009-07-27 20:42  shawnliu  阅读(263)  评论(0编辑  收藏  举报