C#List 排序

原文链接: http://www.cnblogs.com/lx0551/archive/2013/05/13/3075372.html
  1. public class Book  
  2.     {  
  3.         private string name;  
  4.         public string Name  
  5.         {  
  6.             get { return name; }  
  7.             set { name = value; }  
  8.         }  
  9.         private int year;  
  10.         public int Year  
  11.         {  
  12.             get { return year; }  
  13.             set { year = value; }  
  14.         }  
  15.         private int price;  
  16.         public int Price  
  17.         {  
  18.             get { return price; }  
  19.             set { price = value; }  
  20.         }  
  21.         public Book(string name,int year, int price)  
  22.         {  
  23.             Name = name;  
  24.             Year = year;  
  25.             Price = price;  
  26.         }  
  27.     }  
  28.    
  29. public class ComparableBookPriceInc : IComparer<Book>  
  30.     {  
  31.         public int Compare(Book b1, Book b2)  
  32.         {  
  33.             return b1.Price.CompareTo(b2.Price);  
  34.         }  
  35.     }  
  36.    
  37.  public class ComparableBookYearInc : IComparer<Book>  
  38.     {  
  39.         public int Compare(Book b1, Book b2)  
  40.         {  
  41.             return b1.Year.CompareTo(b2.Year);  
  42.         }  
  43.     }  
  44.    
  45.  static void Main(string[] args)  
  46.         {  
  47.             List<Book> listBook = new List<Book>();  
  48.             listBook.Sort(new ComparableBookPriceInc());  
  49.             listBook.Sort(new ComparableBookYearInc());  
  50.          }  

转载于:https://www.cnblogs.com/lx0551/archive/2013/05/13/3075372.html

猜你喜欢

转载自blog.csdn.net/weixin_30883777/article/details/94801659