Wednesday 22 December 2010

List.Sort() alphabetically

I was faced with the challenge today of sorting a list of objects, where one of the properties was a string. My objects were lists of keywords, and I wanted to sort the keywords alphabetically.

I came across some example of lambda expressions, and found that rather than having to write complicated sort methods for my class, I could just do the following:

keywords.Sort((a,b) => String.Compare(a.keyword, b.keyword)):

Awesome, works a treat!