<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: C# Stable Sort</title>
	<atom:link href="http://www.csharp411.com/c-stable-sort/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharp411.com/c-stable-sort/</link>
	<description>C# Information, Code, Tips and News</description>
	<lastBuildDate>Tue, 07 Sep 2010 15:50:57 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Pradeep</title>
		<link>http://www.csharp411.com/c-stable-sort/comment-page-1/#comment-5391</link>
		<dc:creator>Pradeep</dc:creator>
		<pubDate>Tue, 23 Feb 2010 08:46:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/c-stable-sort/#comment-5391</guid>
		<description>Brillaint!!!!! Thanks a lot!!!!!</description>
		<content:encoded><![CDATA[<p>Brillaint!!!!! Thanks a lot!!!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph Johnson Jr.</title>
		<link>http://www.csharp411.com/c-stable-sort/comment-page-1/#comment-5118</link>
		<dc:creator>Joseph Johnson Jr.</dc:creator>
		<pubDate>Thu, 11 Jun 2009 18:52:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/c-stable-sort/#comment-5118</guid>
		<description>BTW, I really like how the author has properly laid out the sort using generic types as it makes other convenient methods possible (such as delegate comparison functions).  For those that do not want to explicitly implement the CompareTo method in their class (let&#039;s say you have various keys you want to sort on determined at runtime, such as the columns in a gridview), you can use the author&#039;s sort method like this:

// or name, etc, determined at runtime.
string SortKey = &quot;age&quot;;
InsertionSort(list, delegate(Person a, Person b) { if (SortKey == &quot;name&quot;) return a.Name.CompareTo(b.Name); else if (SortKey == &quot;age&quot;) return a.Name.CompareTo(b.Name); });

You can see how the anonymous delegate has scope over your SortKey variable which can be the user&#039;s custom sort expression.  Very useful stuff here especially when combined with the power of the .NET GridView control.

If yo uwant to reverse the sort direction, pass in a flag that tells you what the sort direction is (ascending or descending) and simply flip the person objects around: 

bool AscendingSort = false;
// or name, etc, determined at runtime.
string SortKey = &quot;age&quot;; 

InsertionSort(list, delegate(Person a, Person b) { 
if (SortKey == &quot;name&quot;) {
  if (AscendingSort) return a.Name.CompareTo(b.Name); 
  else return b.Name.CompareTo(a.Name);
} else if (SortKey == &quot;age&quot;) {
  if (AscendingSort) return a.Age.CompareTo(b.Age); 
  else return b.Age.ComapreTo(a.Age);
}
return 0;
});</description>
		<content:encoded><![CDATA[<p>BTW, I really like how the author has properly laid out the sort using generic types as it makes other convenient methods possible (such as delegate comparison functions).  For those that do not want to explicitly implement the CompareTo method in their class (let's say you have various keys you want to sort on determined at runtime, such as the columns in a gridview), you can use the author's sort method like this:</p>
<p>// or name, etc, determined at runtime.<br />
string SortKey = "age";<br />
InsertionSort(list, delegate(Person a, Person b) { if (SortKey == "name") return a.Name.CompareTo(b.Name); else if (SortKey == "age") return a.Name.CompareTo(b.Name); });</p>
<p>You can see how the anonymous delegate has scope over your SortKey variable which can be the user's custom sort expression.  Very useful stuff here especially when combined with the power of the .NET GridView control.</p>
<p>If yo uwant to reverse the sort direction, pass in a flag that tells you what the sort direction is (ascending or descending) and simply flip the person objects around: </p>
<p>bool AscendingSort = false;<br />
// or name, etc, determined at runtime.<br />
string SortKey = "age"; </p>
<p>InsertionSort(list, delegate(Person a, Person b) {<br />
if (SortKey == "name") {<br />
  if (AscendingSort) return a.Name.CompareTo(b.Name);<br />
  else return b.Name.CompareTo(a.Name);<br />
} else if (SortKey == "age") {<br />
  if (AscendingSort) return a.Age.CompareTo(b.Age);<br />
  else return b.Age.ComapreTo(a.Age);<br />
}<br />
return 0;<br />
});</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph Johnson Jr.</title>
		<link>http://www.csharp411.com/c-stable-sort/comment-page-1/#comment-5117</link>
		<dc:creator>Joseph Johnson Jr.</dc:creator>
		<pubDate>Thu, 11 Jun 2009 18:31:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/c-stable-sort/#comment-5117</guid>
		<description>Just what I was looking for.  I too ran into the .NET quicksort stability problem.  Insertion sort, although it is of higher time complexity O(N^2) than quicksort&#039;s O(nlogn), ends up being better in practice for smaller collections as there is less overhead (space complexity for one, as this is an in-place algorithm) than, say, merge sort or heap sort.  And of course it&#039;s stable.  Also it is said to be &quot;adaptive&quot; so if the data is &quot;nearly sorted&quot; it can outperform other DAC (divide and conquer) algorithms like merge sort.  In fact you&#039;ll often see &quot;hybrid&quot; sort algorithms using insertion sort when the problem size is small.

For large collections, no contest: merge sort.</description>
		<content:encoded><![CDATA[<p>Just what I was looking for.  I too ran into the .NET quicksort stability problem.  Insertion sort, although it is of higher time complexity O(N^2) than quicksort's O(nlogn), ends up being better in practice for smaller collections as there is less overhead (space complexity for one, as this is an in-place algorithm) than, say, merge sort or heap sort.  And of course it's stable.  Also it is said to be "adaptive" so if the data is "nearly sorted" it can outperform other DAC (divide and conquer) algorithms like merge sort.  In fact you'll often see "hybrid" sort algorithms using insertion sort when the problem size is small.</p>
<p>For large collections, no contest: merge sort.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: List.Sort() uses QuickSort which if two elements are equal, their order might not be preserved &#171; Ezra&#8217;s Blog</title>
		<link>http://www.csharp411.com/c-stable-sort/comment-page-1/#comment-4219</link>
		<dc:creator>List.Sort() uses QuickSort which if two elements are equal, their order might not be preserved &#171; Ezra&#8217;s Blog</dc:creator>
		<pubDate>Wed, 04 Mar 2009 23:24:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/c-stable-sort/#comment-4219</guid>
		<description>[...] http://www.csharp411.com/c-stable-sort/ [...]</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://www.csharp411.com/c-stable-sort/" rel="nofollow">http://www.csharp411.com/c-stable-sort/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Haroon Sarwar</title>
		<link>http://www.csharp411.com/c-stable-sort/comment-page-1/#comment-4133</link>
		<dc:creator>Haroon Sarwar</dc:creator>
		<pubDate>Wed, 21 Jan 2009 05:44:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/c-stable-sort/#comment-4133</guid>
		<description>Check out PowerCollections (http://www.wintellect.com/PowerCollections.aspx) for a stable sort implementation and many other useful functions...

(Source code is available)</description>
		<content:encoded><![CDATA[<p>Check out PowerCollections (<a href="http://www.wintellect.com/PowerCollections.aspx" rel="nofollow">http://www.wintellect.com/PowerCollections.aspx</a>) for a stable sort implementation and many other useful functions&#8230;</p>
<p>(Source code is available)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matjaz</title>
		<link>http://www.csharp411.com/c-stable-sort/comment-page-1/#comment-2845</link>
		<dc:creator>matjaz</dc:creator>
		<pubDate>Thu, 28 Aug 2008 09:29:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/c-stable-sort/#comment-2845</guid>
		<description>Good, but there are much better stable sorts available, for example MergeSort and Binary tree sort, both of which have the same average time complexity as QuickSort (n log n).
See http://en.wikipedia.org/wiki/Stable_sort#Classification</description>
		<content:encoded><![CDATA[<p>Good, but there are much better stable sorts available, for example MergeSort and Binary tree sort, both of which have the same average time complexity as QuickSort (n log n).<br />
See <a href="http://en.wikipedia.org/wiki/Stable_sort#Classification" rel="nofollow">http://en.wikipedia.org/wiki/Stable_sort#Classification</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: uros</title>
		<link>http://www.csharp411.com/c-stable-sort/comment-page-1/#comment-61</link>
		<dc:creator>uros</dc:creator>
		<pubDate>Thu, 24 Jan 2008 11:09:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/c-stable-sort/#comment-61</guid>
		<description>Excellent!</description>
		<content:encoded><![CDATA[<p>Excellent!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
