Wednesday, August 27, 2008

Sorting a List using delegates

I have to sort Lists of Custom classes relatively frequently. Everytime I am about to write the Sort code, I always forget how to do it for a breif second. So as a reminder, I thought I would put a post on how to do it.

Say you have a List of Customer classes.

List customerList = new List();

Now if you want to sort by a property in the list (for example BusinessName) you can do the following:

customerList.Sort( delegate( Customer x, Customer y)
return x.BusinessName.CompareTo(y.BusinessName);



Shaun Austin said...

Hmmm I'd probably want to use the culture specific version is the culture specific version of CompareTo and pass in CultureInfo.CurrentUICulture to ensure localized sorting behaviour at the very least. (FXCop would chew you a new one!!!)

Shaun Austin said...

stoopid iPhone keyboard!!!!!!

Shaun Austin said...

Note to self: stop treating all blog posts like stack overflow questions. There is no rep gained by commenting here!!!!!

Anonymous said...

Dont Bother With Idle Downloads With NZB Files You Can Easily Search Movies, Games, MP3 Singles, Software and Download Them @ Flying Rates


Anonymous said...

Escape Sluggish Downloads With NZB Files You Can Quickly Find Movies, PC Games, MP3 Singles, Software & Download Them at Fast Rates

[URL=][B]Usenet Search[/B][/URL]

Anonymous said...

Infatuation casinos? vouch for this untested [url=]casino[/url] seemly and hold a combat up online casino games like slots, blackjack, roulette, baccarat and more at .
you can also horn in with our redesigned [url=]casino[/url] leadership at and pick up real away misled !
another latest [url=]casino spiele[/url] within an eyelash of is , during german gamblers, project manumitted online casino bonus.

Anonymous said...

Inquire about Our Cheerless Prices at, The Portentous [b][url=]Online Chemist's workshop [/url][/b] To [url=]Buy Viagra[/url] Online ! You Can also Drive to Countless Deals When You [url=]Buy Cialis[/url] and When You You [url=]Buy Levitra[/url] Online. We Also Carry off in a Matchless Generic [url=]Phentermine[/url] On account of Your Nutriment ! We Peddle Tick [url=]Viagra[/url] and Also [url=]Generic Viagra[/url] !