﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum / General .NET Discussion / .NET Forums  / Custom sort algorithm / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>forum@strataframe.net</webMaster><lastBuildDate>Fri, 21 Nov 2008 14:58:39 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Custom sort algorithm</title><link>http://forum.strataframe.net/Topic16116-14-1.aspx</link><description>Here is a quick sample of how to create a collection and the sort the collection with an IComparer class.  This should get you going in the right direction:&lt;/P&gt;&lt;P&gt;[codesnippet]&lt;/P&gt;&lt;P&gt;''' &amp;lt;summary&amp;gt;&lt;BR&gt;''' Create a generic collection that houses the MyItem class in a collection&lt;BR&gt;''' &amp;lt;/summary&amp;gt;&lt;BR&gt;''' &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;&lt;BR&gt;Public Class MyCollection&lt;BR&gt;    Inherits System.Collections.CollectionBase&lt;/P&gt;&lt;P&gt;    Public Sub Add(ByVal item As MyItem)&lt;BR&gt;        InnerList.Add(item)&lt;BR&gt;    End Sub&lt;/P&gt;&lt;P&gt;    ''' &amp;lt;summary&amp;gt;&lt;BR&gt;    ''' Use the IComparer class that we created to sort the list&lt;BR&gt;    ''' &amp;lt;/summary&amp;gt;&lt;BR&gt;    ''' &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;&lt;BR&gt;    Public Sub Sort()&lt;BR&gt;        Dim sorter As New MySort()&lt;BR&gt;        InnerList.Sort(sorter)&lt;BR&gt;    End Sub&lt;BR&gt;End Class&lt;/P&gt;&lt;P&gt;''' &amp;lt;summary&amp;gt;&lt;BR&gt;''' Create the sorter to accept a MyItem class and then sort on the MyValue within the class&lt;BR&gt;''' &amp;lt;/summary&amp;gt;&lt;BR&gt;''' &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;&lt;BR&gt;Public Class MySort&lt;BR&gt;    Implements System.Collections.IComparer&lt;/P&gt;&lt;P&gt;    ''' &amp;lt;summary&amp;gt;&lt;BR&gt;    ''' Compare the two MyValue strings.  If you want to produce a descending sort, then reverse the x and y values.&lt;BR&gt;    ''' &amp;lt;/summary&amp;gt;&lt;BR&gt;    ''' &amp;lt;param name="x"&amp;gt;&amp;lt;/param&amp;gt;&lt;BR&gt;    ''' &amp;lt;param name="y"&amp;gt;&amp;lt;/param&amp;gt;&lt;BR&gt;    ''' &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;BR&gt;    ''' &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;&lt;BR&gt;    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare&lt;BR&gt;        Return String.Compare(DirectCast(x, MyItem).MyValue, DirectCast(y, MyItem).MyValue)&lt;BR&gt;    End Function&lt;/P&gt;&lt;P&gt;End Class&lt;/P&gt;&lt;P&gt;Public Class MyTest&lt;/P&gt;&lt;P&gt;Private _Items As New MyCollection()&lt;/P&gt;&lt;P&gt;Private Sub TestSort()&lt;BR&gt;    _Items.Add(New MyItem("Orange"))&lt;BR&gt;    _Items.Add(New MyItem("Apple"))&lt;BR&gt;    _Items.Add(New MyItem("Pear"))&lt;BR&gt;    _Items.Add(New MyItem("Grapes"))&lt;BR&gt;    _Items.Add(New MyItem("Banana"))&lt;/P&gt;&lt;P&gt;    '-- Sort the collection&lt;BR&gt;    _Items.Sort()&lt;/P&gt;&lt;P&gt;End Sub&lt;/P&gt;&lt;P&gt;End Class&lt;/P&gt;&lt;P&gt;[/codesnippet]</description><pubDate>Mon, 05 May 2008 10:11:31 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>Custom sort algorithm</title><link>http://forum.strataframe.net/Topic16116-14-1.aspx</link><description>Hi,&lt;br&gt;I have item as below:&lt;br&gt;&lt;br&gt;&lt;pre&gt;&lt;br&gt;ItemName          BeforeItem    AfterItem&lt;br&gt;------------------------------------------&lt;br&gt;"First"&lt;br&gt;"Second"&lt;br&gt;"Third"&lt;br&gt;"B4Second"        "Second"&lt;br&gt;"BetweenSecond"   "Second"      "B4Second"&lt;br&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;br&gt;I would like to implement IComparable to sort them so that it could be as below:&lt;br&gt;&lt;br&gt;First&lt;br&gt;B4Second&lt;br&gt;BetweenSecond&lt;br&gt;Second&lt;br&gt;Third&lt;br&gt;&lt;br&gt;Please advice&lt;br&gt;&lt;br&gt;Thank you</description><pubDate>Sun, 04 May 2008 01:37:57 GMT</pubDate><dc:creator>Chan</dc:creator></item></channel></rss>