1

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

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

I have no idea what you're talking about. I just tested the option with the named method and put a breakpoint on the method declaration. Every time the breakpoint was hit, both x and y had values, as expected.
Hello Sir. Something is wrong with this. The array does not consist of a null value after the first step on the code, but on the next step, the y value is nothing.and return this Error The value cannot be null.
No Sir, Each time the breakpoint will hit the x and y, the value of x is the same and the value of y is nothing.
I have tested this code multiple times and that doesn't happen for me so I can only assume that you did something wrong. If you don't show us what you did, we can't tell you what's wrong with it. I'm guessing that you changed the data and my algorithm doesn't work in the general case. I already said that I was guessing at the actual rules you wanted to implement so maybe update your question with ALL the relevant information that you should have provided in the first place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.