StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      



Custom sort algorithmExpand / Collapse
Author
Message
Posted 05/04/2008 1:37:57 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: Today @ 9:51:36 AM
Posts: 429, Visits: 1,612
Hi,
I have item as below:


ItemName BeforeItem AfterItem
------------------------------------------
"First"
"Second"
"Third"
"B4Second" "Second"
"BetweenSecond" "Second" "B4Second"



I would like to implement IComparable to sort them so that it could be as below:

First
B4Second
BetweenSecond
Second
Third

Please advice

Thank you
Post #16116
Posted 05/05/2008 10:11:31 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Today @ 1:19:33 PM
Posts: 4,556, Visits: 4,542
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:

''' <summary>
''' Create a generic collection that houses the MyItem class in a collection
''' </summary>
''' <remarks></remarks>
Public Class MyCollection
    Inherits System.Collections.CollectionBase

    Public Sub Add(ByVal item As MyItem)
        InnerList.Add(item)
    End Sub

    ''' <summary>
    ''' Use the IComparer class that we created to sort the list
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Sort()
        Dim sorter As New MySort()
        InnerList.Sort(sorter)
    End Sub
End Class

''' <summary>
''' Create the sorter to accept a MyItem class and then sort on the MyValue within the class
''' </summary>
''' <remarks></remarks>
Public Class MySort
    Implements System.Collections.IComparer

    ''' <summary>
    ''' Compare the two MyValue strings.  If you want to produce a descending sort, then reverse the x and y values.
    ''' </summary>
    ''' <param name="x"></param>
    ''' <param name="y"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Return String.Compare(DirectCast(x, MyItem).MyValue, DirectCast(y, MyItem).MyValue)
    End Function

End Class

Public Class MyTest

Private _Items As New MyCollection()

Private Sub TestSort()
    _Items.Add(New MyItem("Orange"))
    _Items.Add(New MyItem("Apple"))
    _Items.Add(New MyItem("Pear"))
    _Items.Add(New MyItem("Grapes"))
    _Items.Add(New MyItem("Banana"))

    '-- Sort the collection
    _Items.Sort()

End Sub

End Class

Post #16139
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Ben Chase, Trent L. Taylor, Steve L. Taylor

PermissionsExpand / Collapse

All times are GMT -6:00, Time now is 8:42pm

Powered by InstantForum.NET v4.1.4 © 2008
Execution: 0.109. 10 queries. Compression Enabled.
Site Map - Home - My Account - Forum - About Us - Contact Us - Try It - Buy It

Microsoft, Visual Studio, and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.