Page 1 of 1

Custom Sorting Implement an IComparer.Compare Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Posted 14 July 2008 - 01:33 PM

Start a new windows project
Add a button and listbox
Insert the following code into form1
Public Class Form1


     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a() As String = {"4", "5", "10", "12", "14", "15", "7", "8", "9", "3", "18", "16", "15", "100", "13", "55"}
        Dim sc As New NumericComparer(True)
        Array.Sort(a, sc)
        ListView1.Items.Clear()
        For i As Integer = 0 To a.Length - 1
            ListBox1.Items.Add(a(i))
        Next
    End Sub
End Class



Implementing the IComparer.Compare
Add a new class insert following code.

Public Class NumericComparer
    Implements IComparer
    Dim mText As Boolean = False
    Public Sub New(ByRef sorttype As NumericSort)
        mText = sorttype
    End Sub

    Public Enum NumericSort
        Textual = True
        Numerical = False
    End Enum

    ' Compare the two employees.
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
    Implements System.Collections.IComparer.Compare
        If mText = False Then
            Dim txtx As Double = CDbl(x)
            Dim txty As Double = CDbl(y)
            Select Case True
                Case txtx < txty : Return -1
                Case txtx = txty : Return 0
                Case txtx > txty : Return 1
            End Select
        Else
            Dim txtx As String = CStr(x)
            Dim txty As String = CStr(y)
            Return String.Compare(txtx, txty)
        End If

    End Function
End Class



Is This A Good Question/Topic? 1
  • +

Replies To: Custom Sorting

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Posted 19 August 2009 - 09:05 PM

Sort Email Address by Domain then ID.
Public Class EmailSorter
 Implements IComparer(Of String)
 Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
  Dim IndexOfAckInX As Integer = x.IndexOf("@")
  Dim IndexOfAckInY As Integer = y.IndexOf("@")
  Select Case True
   Case IndexOfAckInX = -1 AndAlso IndexOfAckInY = -1
	' x & y don't contains @
	Return x.CompareTo(y)
   Case IndexOfAckInX = -1 AndAlso IndexOfAckInY > -1
	' only y contains @
	Return -1
   Case IndexOfAckInX > -1 AndAlso IndexOfAckInY = -1
	' Only x contains @
	Return +1
   Case IndexOfAckInX > -1 AndAlso IndexOfAckInY > -1
	' Both x & y contain @
	Dim xs As String() = x.Split("@")
	Dim ys As String() = y.Split("@")
	' Compare domains
	Select Case xs(1).CompareTo(ys(1))
	 Case 0
	  ' Same Domain, so compare address.
	  Return xs(0).CompareTo(ys(0))
	 Case -1 : Return -1
	 Case +1 : Return +1
	End Select
  End Select
 End Function
End Class


Was This Post Helpful? 2
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Posted 24 December 2009 - 05:03 PM

Explaining IComparer Interface

When a class implement the ICompare, you're stating how you would like 2 objects to be compared.
Public Class NumericComparer
	Implements IComparer

 Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

 End Function
End Class


The Compare Function will return one of three basic values, depending on how X relates to Y.

-1  (<0) Object X  is before Object Y
 0	   Object X  is in equal position as Object Y
+1 (>0)  Object X  is after Object Y


I hope this additional information make the previous two examples a little clearer.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1