2015/01/07

How to sort list of object by property with linq C# ?

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
List<Person> list = new List<Person>();
list.Add(new Person() { FirstName = "Mike", LastName = "Smith" });
list.Add(new Person() { FirstName = "John", LastName = "Fiolkowsky" });
list.Add(new Person() { FirstName = "Andrew", LastName = "Dawson" });
list.Sort((p1, p2) => p1.LastName.CompareTo(p2.LastName));
view raw gistfile1.cs hosted with ❤ by GitHub