转自:http://blog.sina.com.cn/s/blog_486a8cc50102uwxn.html
Using delegates
class Book
{
public Book(string id, string name, string author)
{
ID=id;
Name=name;
Author=author;
}
public string ID{ get; set;}
public string Name{get; set;}
public string Author{get; set;}
}
List listBook = new List();
listBook.Add(new Book("103","Code Complete","Steve MC"));
listBook.Add(new Book("101","Effective C++","Scott Meyers"));
listBook.Add(new Book("102","CLR Via C#","Jeff Prosise"));
listBook.Sort(
delegate(Book a, Book b)
{
return a.ID.CompareTo(b.ID);
});
Using Comparator
static in CompareBook(Book a, Book b)
{
return a.ID.CompareTo(b.ID);
}
listBook.Sort(CompareBook);
Using IComparable
class Book : IComparable
{
public Book(string id, string name, string author)
{
ID = id;
Name = name;
Author = author;
}
public string ID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public int CompareTo(object obj)
{
return ID.CompareTo(((Book)obj).ID);
}
}