<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elegant Code &#187; sort observable collection</title>
	<atom:link href="http://elegantcode.com/tag/sort-observable-collection/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 04:40:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Write a Sortable ObservableCollection for WPF</title>
		<link>http://elegantcode.com/2009/05/14/write-a-sortable-observablecollection-for-wpf/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=write-a-sortable-observablecollection-for-wpf</link>
		<comments>http://elegantcode.com/2009/05/14/write-a-sortable-observablecollection-for-wpf/#comments</comments>
		<pubDate>Thu, 14 May 2009 14:12:06 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[sort collection]]></category>
		<category><![CDATA[sort observable collection]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/14/write-a-sortable-observablecollection-for-wpf/</guid>
		<description><![CDATA[You probably have had the need to sort an ObservableCollection at some point in one of your applications by either ascending or descending order.&#160; Of course, you can always use the ObservableCollection.OrderBy and ObservableCollection.OrderByDescending, but these methods just return a new collection of IOrderedEnumerable, which forces you have to rebind the DataContext/ItemsSource in your UI, [...]]]></description>
			<content:encoded><![CDATA[<p>You probably have had the need to sort an ObservableCollection at some point in one of your applications by either ascending or descending order.&#160; Of course, you can always use the ObservableCollection.OrderBy and ObservableCollection.OrderByDescending, but these methods just return a new collection of IOrderedEnumerable, which forces you have to rebind the DataContext/ItemsSource in your UI, which could be a pain.&#160; So instead, wouldn’t it be nice to have an ObservableCollection that you could call a Sort method on, give it a lambda to sort on and a direction, then have it perform the sort without having to rebind to the UI.&#160; Well, it is not a difficult as you may think.&#160; So lets go ahead and write one.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 600px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SortableObservableCollection&lt;T&gt; : ObservableCollection&lt;T&gt;
{
  <span style="color: #0000ff">public</span> SortableObservableCollection(List&lt;T&gt; list)
     : <span style="color: #0000ff">base</span>(list)
  {
  }

  <span style="color: #0000ff">public</span> SortableObservableCollection(IEnumerable&lt;T&gt; collection)
     : <span style="color: #0000ff">base</span>(collection)
  {
  }

  <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Sort&lt;TKey&gt;(Func&lt;T, TKey&gt; keySelector, System.ComponentModel.ListSortDirection direction)
  {
     <span style="color: #0000ff">switch</span> (direction)
     {
        <span style="color: #0000ff">case</span> System.ComponentModel.ListSortDirection.Ascending:
           {
              ApplySort(Items.OrderBy(keySelector));
              <span style="color: #0000ff">break</span>;
           }
        <span style="color: #0000ff">case</span> System.ComponentModel.ListSortDirection.Descending:
           {
              ApplySort(Items.OrderByDescending(keySelector));
              <span style="color: #0000ff">break</span>;
           }
     }
  }

  <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Sort&lt;TKey&gt;(Func&lt;T, TKey&gt; keySelector, IComparer&lt;TKey&gt; comparer)
  {
     ApplySort(Items.OrderBy(keySelector, comparer));
  }

  <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> ApplySort(IEnumerable&lt;T&gt; sortedItems)
  {
     var sortedItemsList = sortedItems.ToList();

     <span style="color: #0000ff">foreach</span> (var item <span style="color: #0000ff">in</span> sortedItemsList)
     {
        Move(IndexOf(item), sortedItemsList.IndexOf(item));
     }
  }
}</pre>
</div>
<p>Now we can create a new SortableObservableCollection&lt;T&gt; and sort it either direction without having to return a new IOrderedEnumerable and rebind the DataContext/ItemsSource.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #008000">//sort ascending</span>
MySortableList.Sort(x =&gt; x.Name, ListSortDirection.Ascending);

<span style="color: #008000">//sort descending</span>
MySortableList.Sort(x =&gt; x.Name, ListSortDirection.Descending);</pre>
</div>
<p>When you sort your collection, the UI is notified that something has changed and updates itself automatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/14/write-a-sortable-observablecollection-for-wpf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

