I am making a program which has two arrays, both are strings. I want to sort the second string alphabetically and the first string needs to go with the second string. I then need to display them both, with the secodn string in alphabetical order with the appropiate name(first string) next to it.

Recommended Answers

All 3 Replies

What have you done so far? Do you understand how arrays work? Have you chosen a sorting algorithm?

VB array.sort only works with single dimension array, so quickest way would be to use the IComparer function to allow VB.net array sort function to work with your custom array structure.

create an array structure (or you could use a class) to hold both your strings in a single object variable
eg..........

Public Class MainForm
    Public Structure twoStrings
        Public string1 As String
        Public string2 As String
    End Structure

    Sub main()
        'place all your strings into the new duel array
        Dim stringdata(100) As twoStrings

        stringdata(0).string1 = "unsorted string"
        stringdata(0).string2 = "string to sort" '.......etc
        ' stringdata(1).string1 ............

        'and then to sort the data, use
        Array.Sort(stringdata, New SorttwoStrings) 'use sorttwostrings class
    End Sub

    Public Class SorttwoStrings
        'helper routine to sort by structured string value
        Implements IComparer(Of twoStrings)

        Public Function Compare(ByVal s1 As twoStrings, ByVal s2 As twoStrings) As Integer_ Implements System.Collections.Generic.IComparer(Of twoStrings).Compare
            'sort by string2
            If s1.string2 > s2.string2 Then
                Return -1
            ElseIf s2.string2 < s1.string2 Then
                Return 1
            Else
                Return 0 ' same value
            End If
        End Function
    End Class
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.