'未能比较数组中的两个元素'对于SortedList

时间:2014-03-27 10:39:27

标签: vb.net compare sortedlist

使用我编写的协议生成工具的人之一报告了一个我无法理解的错误:

Failed to compare two elements in the array. 

我使用SortedList来保存协议的所有问题,并在特殊的比较器中使用以下Sort方法对它们进行排序:

Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare

    Dim val1BiggerVal2 As Integer = 0

    Dim subkeysVal1 As String() = x.Split("_"c)
    Dim subkeysVal2 As String() = y.Split("_"c)
    Dim rxNumberOnly As Regex = New Regex("^[0-9]+$")
    Dim leftTaskgroup, leftParent, leftKey, leftID, leftCompstring, rightTaskgroup, rightParent, rightKey, rightID, rightCompstring As String
    leftCompstring = ""
    For counter As Integer = 0 To subkeysVal1.Length - 5
        leftCompstring &= subkeysVal1(counter)
    Next
    leftTaskgroup = subkeysVal1(subkeysVal1.Length - 4)
    leftParent = subkeysVal1(subkeysVal1.Length - 3)
    leftKey = subkeysVal1(subkeysVal1.Length - 2)
    leftID = subkeysVal1(subkeysVal1.Length - 1)

    rightCompstring = ""
    For counter As Integer = 0 To subkeysVal1.Length - 5
        rightCompstring &= subkeysVal2(counter)
    Next
    rightTaskgroup = subkeysVal2(subkeysVal2.Length - 4)
    rightParent = subkeysVal2(subkeysVal2.Length - 3)
    rightKey = subkeysVal2(subkeysVal2.Length - 2)
    rightID = subkeysVal2(subkeysVal2.Length - 1)

    val1BiggerVal2 = compareSubstring(leftCompstring, rightCompstring)
    'If Components where the same, check Taskgroups
    If (val1BiggerVal2 = 0) Then
        val1BiggerVal2 = compareSubstring(leftTaskgroup, rightTaskgroup)
    End If
    'If Taskgroups where the same, check parent-child relation
    If (val1BiggerVal2 = 0) Then
        If rightKey = leftParent Then
            val1BiggerVal2 = 1
        ElseIf rightParent = leftKey Then
            val1BiggerVal2 = -1
        ElseIf rightParent = leftParent Then
            val1BiggerVal2 = compareSubstring(leftKey, rightKey)
        Else
            Dim Left As String = leftParent
            Dim Right As String = rightParent
            If leftParent = "" Then
                Left = leftKey
            End If
            If rightParent = "" Then
                Right = rightKey
            End If

            val1BiggerVal2 = compareSubstring(Left, Right)
        End If
    End If
   'if even the parent-child relation wasnt unquie, use the autoincremented endvalue
    If val1BiggerVal2 = 0 Then
        val1BiggerVal2 = leftID.compareTo(rightID)
    End If
    Return val1BiggerVal2 

End Function

我认为在最后一次检查中,如果val1biggerVal2为0,我应该始终获得唯一标识,因为每次从输入文件中读取问题时,最后_ID会自动增加。

1 个答案:

答案 0 :(得分:1)

经过几个小时的思考,没有进一步的线索,我决定只用SortedList数据结构替换SortedDictionary。 这解决了问题,虽然如果有人知道为什么版本SortedList不起作用,请告诉我。