如何正确排序此结构?
struct Person
{
public string Name;
public int Age;
}
List<Person> People = new List<Person>();
// Add several hundred records
// sort by age
People.Sort(Person.Age);
最佳答案
您可以在此处使用lambda 表达式以及泛型:
struct Person {
public string Name;
public int Age;
}
// generic List<T> is much better than deprecated List
List<Person> People = new List<Person>();
...
People.Sort((x, y) => x.Age - y.Age);
另一个流行的解决方案是Linq,但它创建一个新列表,因此可能效率不高:
People = People.OrderBy(x => x.Age).ToList();
关于c# - 如何对具有特定结构元素的结构列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22437031/