在VB中排序字符串数组的问题

时间:2011-01-13 21:33:12

标签: vb.net

有没有办法在VB中对数组进行排序,将J10和J11放在J9之后?

J1    (PN= 605848)         
J10   (PN= 605987)         
J11   (PN= 605987)         
J2    (PN= 605848)         
J3    (PN= 605836)         
J4    (PN= 605848)         
J5    (PN= 605848)         
J6    (PN= 605848)         
J7    (PN= 605189)         
J7B   (PN= 605189)         
J7E   (PN= 605189)         
J7F   (PN= 605189)         
J7I   (PN= 605189)         
J7J   (PN= 605189)         
J7M   (PN= 605189)         
J7N   (PN= 605189)         
J8    (PN= 605987)         
J9    (PN= 605987)    

这是我运行myArray.sort()

后得到的

谢谢。

1 个答案:

答案 0 :(得分:1)

假设您正在使用某些版本的VB.Net:

MicSim是对的,你需要使用自己的比较器。这是如何做到的。

比较器是inherits Comparer(Of 某事 )的任何类。在您的情况下,它应该继承Comparer(Of String),因为您要排序的值是字符串。

' If this class is frequently used, it should be in it's own source file.
' If it's used only in one place, put it at the end of that source file.

' This class is used to compare strings of the form L123
'   where L is a letter A-Z
'   and 123 is an integer
' The goal here is to make sure that "L9" comes before "L10",
' which isn't what we would get with simple string comparisons.
Class CompLetterInteger
    Inherits Comparer(Of String)
    Public Overrides Function Compare(ByVal x As String, ByVal y As String) As Integer
        ' Can value "Nothing" be in the array?
        ' This makes sure that they come at the beginning of the array.
        ' Skip these 8 lines if value Nothing is impossible.
        If x Is Nothing Then
            If y Is Nothing Then
                Return 0 ' Values sort the same
            End If
            Return -1 ' X comes before Y
        ElseIf y Is Nothing Then
            Return 1 ' Y comes before X
        End If

        ' Here we parse both arguments into the first letter,
        ' and then the numeric part. You might have to adjust
        ' this for your data - if value "J123X" is possible,
        ' you're going to have to adjust this.
        Dim x1 As String = x.Substring(0, 1)
        Dim x2 As Integer = 0
        Integer.TryParse(x.Substring(1), x2)

        Dim y1 As String = y.Substring(0, 1)
        Dim y2 As Integer = 0
        Integer.TryParse(y.Substring(1), y2)

        ' Now decide which value should come first.
        ' -1 means that X should come first,
        ' +1 means that Y should come first,
        '  0 means that they sort the same.
        If x1 < y1 Then Return -1 ' The letter of X is before the letter of Y
        If x1 > y1 Then Return 1 ' The letter of X is after the letter of Y
        ' The letters are equal, so look at the numeric part
        If x2 < y2 Then Return -1 ' The number of X is less than the number of Y
        If x2 > y2 Then Return 1 ' The number of X is more than the number of Y
        Return 0    ' The two strings sort the same
        ' Note that this does not mean that the two strings are identical.
        ' "Y99" would sort the same as "Y099",
        ' because the letters are the same and the numbers are the same value.
    End Function
End Class

实际排序数组:

Dim Arr() As String = {"J1", "J9", "J10", "J11"} ' Etc.
Array.Sort(Arr, New CompLetterInteger)