下面我创建了一个包含4个Person类型元素的列表。我想根据Age属性按升序对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;
}
}
}
}
答案 0 :(得分:3)
试试这个:
#include <stdio.h>
int main()
{
char *str;
str = malloc(4096);
gets(str);
puts(str);
free(str);
return (0);
}
答案 1 :(得分:3)
people.Sort((p1, p2) =>
{
return p1.Age - p2.Age;
});