<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Derik Whittaker : Linq, Lambda</title><link>http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/Lambda/default.aspx</link><description>Tags: Linq, Lambda</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Sorting List's the .Net 3.5 Way</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/sorting-list-s-the-net-3-5-way.aspx</link><pubDate>Sat, 29 Mar 2008 02:09:13 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39854</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=39854</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=39854</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/sorting-list-s-the-net-3-5-way.aspx#comments</comments><description>&lt;p&gt;This post is another in the recent string of How-to&amp;#39;s.&amp;nbsp; But it is just so hard to resist.&amp;nbsp; .Net 3.5 has so many cool features, it is just fun to explore them all.&lt;/p&gt; &lt;p&gt;Ok, so here is the issue we are going to solve today.&amp;nbsp; We have a list of entities and we need to sort them.&amp;nbsp; In this post I will show you 3 different ways to accomplish this task, all producing the same result, but using different techniques (hey &lt;a href="http://msmvps.com/blogs/peterritchie/default.aspx" target="_blank"&gt;Peter&lt;/a&gt;, go ahead and show me a 4th and better way :) ).&lt;/p&gt; &lt;p&gt;A little up front declarations.&amp;nbsp; Each method will use the same list, so to save space I will declare this now.&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;List userList = new List
    {
        new User {FirstName = &amp;quot;Andy&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Opie&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Bee&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Barney&amp;quot;, LastName = &amp;quot;Fife&amp;quot;},
        new User {FirstName = &amp;quot;Goomer&amp;quot;, LastName = &amp;quot;Pile&amp;quot;}
};
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 1 -- Using the Sort with IComarer&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;public void Sort_OldSchool()
{
    userList.Sort(new UserCompare());
}

// Sort class
private class UserCompare : IComparer&amp;lt;User&amp;gt;
 {
    public int Compare( User x, User y )
    {
    return x.LastName.CompareTo( y.LastName );
}
&lt;/pre&gt;
&lt;p&gt;The above works, but requires you to create a custom class that implements the IComparer&amp;lt;User&amp;gt; interface to do the trick.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 2 -- Using Lambda and extension methods&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;[Test]
public void Sort_UsingLambda()
{
	userList = userList.OrderBy( user =&amp;gt; user.LastName ).ToList();
}
&lt;/pre&gt;
&lt;p&gt;This way works great, it is clean and very readable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 3 -- Use Linq2Objects&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;[Test]
public void Sort_UsingLinq()
{
	userList = ( from u in userList
        	orderby u.LastName, u.FirstName
                select u ).ToList();
}
&lt;/pre&gt;
&lt;p&gt;By using Linq you open yourself up to a whole new level of flexibility. You can not only sort (OrderBy), you can filter, return only a select number of rows, etc, etc.&amp;nbsp; Basically you can do anything that Linq provides.&lt;/p&gt;
&lt;p&gt;As you can see from the above, they all accomplish the same goal but method 2 &amp;amp; 3 are just more fun.&amp;nbsp; Besides, if you are going to be using .Net 3.5 you might as well use the new features.&lt;/p&gt;
&lt;p&gt;BTW, all the methods (except 3 since it sorts by LastName and FirstName) above produce the following output.&lt;br /&gt;&lt;em&gt;Barney Fife&lt;br /&gt;Goomer Pile&lt;br /&gt;Andy Tayler&lt;br /&gt;Opie Tayler&lt;br /&gt;Bee Tayler&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39854" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net/default.aspx">.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Opinion/default.aspx">Opinion</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Lambda/default.aspx">Lambda</category></item></channel></rss>