admin管理员组文章数量:1636976
转自:http://blog.sina/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);
}
}
本文标签: objectimplementIComparable
版权声明:本文标题:At least one object must implement IComparable 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729233098a1191677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论