I'm CaLendarW Blog

Tag: sort

List.Sort – Lambda Expression Way

by calendarw on Aug.18, 2009, under coding snippet

呢期研究既 delegation, 其中一個例子係 Lambda 在 List.Sort 既使用.

List<Person> persons = new List<Person>();

// sort by name
persons.Sort((a,b) => a.Name.CompareTo(b.Name));

// sort by age
persons.Sort((a,b) => a.Age.CompareTo(b.Age));

bool ascending = false;

// descending by age
persons.Sort((a,b) => a.Age.CompareTo(b.Age) * (ascending ? 1 : -1));

有了這樣既 syntax, 寫 sorting 易左同快捷左很多~~

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!