开发者

Issues sorting an array of strings in VB

开发者 https://www.devze.com 2023-02-04 10:39 出处:网络
Is there a way to sort an array in VB that would put J10 and J11 after J9? J1(PN= 605848) J10(PN= 605987)

Is there a way to sort an array in VB that would put J10 and J11 after 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)         开发者_如何学Python
J7E   (PN= 605189)         
J7F   (PN= 605189)         
J7I   (PN= 605189)         
J7J   (PN= 605189)         
J7M   (PN= 605189)         
J7N   (PN= 605189)         
J8    (PN= 605987)         
J9    (PN= 605987)    

This is what I get after I run myArray.sort()

Thank you.


Assuming you're using some version of VB.Net:

MicSim was right, you need to use your own Comparator. Here's how to do it.

The Comparator is any class that inherits Comparer(Of something ). In your case, it should inherit Comparer(Of String) because the values you want to sort are strings.

' 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

To actually sort the array:

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

精彩评论

暂无评论...
验证码 换一张
取 消