Custom sort algorithm


Author
Message
Chan
Chan
Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)Advanced StrataFrame User (701 reputation)
Group: Forum Members
Posts: 533, Visits: 2K
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
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search