Levesque Xylia
Levesque Xylia

Reputation: 369

Sort Alphanumeric String inside array

I have a String Array in my vb.net code. Like this:

arrayPartMarks
-------------
C90X61Y13B
C90X61Y13D
C90X61Y1B
C90X61Y1D
-------------

I want to sort it by ascending order like this.

arrayPartMarks
-------------
C90X61Y1B
C90X61Y13B
C90X61Y1D
C90X61Y13D  
-------------

I try to sort it using Array.

Array.Sort(arrayPartMarks)

and the result is the same:

arrayPartMarks
-------------
C90X61Y13B
C90X61Y13D
C90X61Y1B
C90X61Y1D
-------------

Is there anyway to sort it?

Thank you in Advance

Upvotes: 1

Views: 380

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

I will provide a general solution and it is then up to you provide the specific details because you have only provided us with one example and that doesn't explain every detail as required for an actual algorithm. (EDIT: I actually got much more specific than I first intended)

Firstly, there's nothing inbuilt that will do that for you, so you have to provide the comparison logic yourself. There are a few ways that you can do that but the simplest is by using a Comparison(Of T) delegate, e.g.

Array.Sort(myArray, Function(x, y)
                        If x < y Then
                            Return -1
                        ElseIf x > y Then
                            Return 1
                        Else 'x = y
                            Return 0
                        End If
                    End Function)

The Comparison(Of T) delegate references a function that takes two objects of type T and returns an Integer that represents their relative magnitude. If the first object is considered less than the second then the return value must be less than zero and if the first object is considered greater than the second then the return value must be greater than zero. If the two objects are considered equal then return zero. The positive and negative return values are usually 1 and -1 respectively but they don't have to be. That means that, for instance, they could be the result of a subtraction of a specific numeric property, e.g.

Array.Sort(myArray, Function(x, y) x.SomeIntegerProperty - y.SomeIntegerProperty)

Based on your example, it looks like you could take the characters after the first eight and then sort initially by the last letter and, when that's the same, sort by the preceding digit where there is one. That might look like this:

Array.Sort(myArray, Function(x, y)
                        'Get the last letter.
                        Dim xLetter = x.Last()
                        Dim yLetter = y.Last()

                        'Sort by letter if they are different.
                        If xLetter < yLetter Then
                            Return -1
                        ElseIf xLetter > yLetter Then
                            Return 1
                        End If

                        'Get the characters after the common prefix.
                        Dim xSuffix = x.Substring(8)
                        Dim ySuffix = y.Substring(8)

                        'Get a number from the suffix, zero if no digit present.
                        Dim xNumber = Val(xSuffix)
                        Dim yNumber = Val(ySuffix)

                        'Sort by number.
                        Return Math.Sign(xNumber - yNumber)
                    End Function)

Note that I have used a Lambda expression here but you could also use a conventional method. The method would be written like so:

Private Function CompareStrings(x As String, y As String) As Integer
    'Get the last letter.
    Dim xLetter = x.Last()
    Dim yLetter = y.Last()

    'Sort by letter if they are different.
    If xLetter < yLetter Then
        Return -1
    ElseIf xLetter > yLetter Then
        Return 1
    End If

    'Get the characters after the common prefix.
    Dim xSuffix = x.Substring(8)
    Dim ySuffix = y.Substring(8)

    'Get a number from the suffix, zero if no digit present.
    Dim xNumber = Val(xSuffix)
    Dim yNumber = Val(ySuffix)

    'Sort by number.
    Return Math.Sign(xNumber - yNumber)
End Function

and then used like so:

Array.Sort(myArray, AddressOf CompareStrings)

Upvotes: 3

Related Questions