Wednesday, May 27, 2009

list<> sort or order using limbda expression

If doing an in-place sort (i.e. the list is updated):
people.Sort((x, y) => string.Compare(x.LastName, y.LastName));

If need to create a new list:
var newList = people.OrderBy(x=>x.LastName)

Springboard example, sorting sections on section No.

List
sections = new List
();
//... load sections ...
sections.Sort((x, y) => x.SectionNo.CompareTo(y.SectionNo));

No comments: