Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using IComparer Class

Status
Not open for further replies.

Iamthestig

Programmer
Apr 30, 2008
38
GB
Hi,

I have a column in a Janus datagrid that shows dates as a string in the format 'mm/yyyy'.

04/2004
03/2006
01/2005
03/2005
01/2008

When I sort this column by clicking the column header I get this result

01/2005
01/2008
03/2005
03/2006
04/2004

which is obviously not want I want. I have been told I need to implement the IComparer interface and use an instance of that class as the SortComparer property of the column. For example:

Me.GridEX1.RootTable.Columns("X").GroupComparer = New MyCustomComparer()

Does anyone know how I go about constructing this class in VB.Net? I would normally try and work this out myself but I'm short of time. Many thanks,

John
 
For those of you interested, I cracked it. Not very elegant but it works.

Code:
Public Class CustomSortCriteria
        Implements IComparer
        Public Overridable Overloads Function Compare(ByVal Obj1 As Object, _
              ByVal Obj2 As Object) As Integer Implements IComparer.Compare

            ' Take the two objects and convert into strings
            Dim str1 As String = Trim(CType(Obj1, String))
            Dim str2 As String = Trim(CType(Obj2, String))

            If str1 = "" Then str1 = "01/1900"
            If str2 = "" Then str2 = "01/1900"

            str1 = Microsoft.VisualBasic.Right(str1, 4) + Microsoft.VisualBasic.Left(str1, 2)
            str2 = Microsoft.VisualBasic.Right(str2, 4) + Microsoft.VisualBasic.Left(str2, 2)

            ' Compare the strings and return the compare value
            Return String.Compare(str1, str2)

        End Function
    End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top