【发布时间】:2012-03-21 11:34:23
【问题描述】:
我正在编写一个地址簿程序。我将每个人的详细信息存储在List<Person> 中。我需要能够按姓氏(如果有联系,则使用名字)或邮政编码对这个列表进行排序。
到目前为止,我有这个:
public class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string PostCode { get; set; }
// etc..
}
public class AddressBook
{
public List<Person> People { get; set; }
// asc: ascending or descending
// column: the property to use when sorting
// (in my case either LastName or Postcode)
public void Sort(bool asc, string column)
{
// What should I put here?
}
// etc...
}
我尝试过使用ICompare 和IComparable 接口,但我就是不明白。
Sort方法怎么写?
【问题讨论】:
-
您使用的是什么版本的 C# / .NET?
-
我试图清理你的问题,以便更容易阅读。希望没事。
标签: c# sorting generic-list