Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
445
Natural Sort Error
posted

I am trying to do a natural sort on a column in the WinListview and receiving the Following error:

IComparer (or the IComparable methods it relies upon) did not return zero when Array.Sort called x. CompareTo(x). x: 'ListViewItem: {Key[322], Text[Service Address]}'  x's type: 'UltraListViewItem' The IComparer: 'Infragistics.Win.UltraWinListView.DefaultItemsCollectionSortComparer'.

I am only using this sort on one column and setting that columns SortComparer property to my new class.

I translated the code from C# but I'm  pretty sure I got the translation right.

Any and all help would be greatly appreciated.

Here is my code:

    Public Class AlphaNumComparer
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Try
            Dim x1 As Infragistics.Win.UltraWinListView.UltraListViewItem = x          
            If x1 Is Nothing OrElse x1.SubItems("AddressNumber").Value Is Nothing OrElse x1.SubItems("AddressNumber").Value.ToString = "" Then Return 0

            Dim s1 As String = x1.SubItems("AddressNumber").Value.ToString   'as string;
            If s1 Is Nothing Then Return 0

            Dim y1 As Infragistics.Win.UltraWinListView.UltraListViewItem = y
            If y1 Is Nothing OrElse y1.SubItems("AddressNumber").Value Is Nothing OrElse y1.SubItems("AddressNumber").Value.ToString = "" Then Return 0

            Dim s2 As String = y1.SubItems("AddressNumber").Value.ToString  'as string;
            If s2 Is Nothing Then Return 0

            Dim len1 As Integer = s1.Length
            Dim len2 As Integer = s2.Length
            Dim marker1 As Integer = 0
            Dim marker2 As Integer = 0

            ' Walk through two the strings with two markers.
            Do While marker1 < len1 And marker2 < len2

                Dim ch1 As Char = s1(marker1)
                Dim ch2 As Char = s2(marker2)

                'Some buffers we can build up characters in for each chunk.
                Dim space1(len1) As Char '= New Char(len1)
                Dim loc1 As Integer = 0
                Dim space2(len2) As Char '= New Char(len2)
                Dim loc2 As Integer = 0

                ' Walk through all following characters that are digits or
                ' characters in BOTH strings starting at the appropriate marker.
                ' Collect char arrays.
                Do
                    space1(loc1) = ch1
                    marker1 += 1
                    If (marker1 < len1) Then
                        ch1 = s1(marker1)
                    Else
                        Exit Do
                    End If
                Loop While (Char.IsDigit(ch1) = Char.IsDigit(space1(0)))

                Do
                    loc2 += 1
                    space2(loc2) = ch2
                    marker2 += 1

                    If (marker2 < len2) Then
                        ch2 = s2(marker2)
                    Else
                        Exit Do
                    End If
                Loop While (Char.IsDigit(ch2) = Char.IsDigit(space2(0)))

                ' If we have collected numbers, compare them numerically.
                ' Otherwise, if we have strings, compare them alphabetically.
                Dim str1 As String = New String(space1)
                Dim str2 As String = New String(space2)

                Dim result As Integer

                If Char.IsDigit(space1(0)) And Char.IsDigit(space2(0)) Then
                    Dim thisNumericChunk As Integer = Integer.Parse(str1)
                    Dim thatNumericChunk As Integer = Integer.Parse(str2)
                    result = thisNumericChunk.CompareTo(thatNumericChunk)

                Else

                    result = str1.CompareTo(str2)
                End If


                If result <> 0 Then
                    Return result
                End If

            Loop

            Return len1 - len2
        Catch ex As Exception

        End Try

    End Function
End Class

  • 69832
    Verified Answer
    Offline posted

    As the error message indicates, you need to handle the case where the x and y parameters reference the same object. You return zero in that case to indicate that they are equal.