下面我创建了一个由4个Person类型元素组成的列表。我想要按照年龄属性对Person列表进行升序排列。是否有一种优雅的方法使用LINQ或IComparable(或其他方式)实现这一点,而不必从头编写自己的算法?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Person> people = new List<Person>();
people.Add(new Person("Matthew", 27));
people.Add(new Person("Mark", 19));
people.Add(new Person("Luke", 30));
people.Add(new Person("John", 20));
// How to sort list by age?
}
private class Person
{
string Name { get; set; }
int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
}
}