简体   繁体   中英

How to sort a 2 dimensional array alphabetically

I am trying to learn Visual Basic, and so I decided to create a simple program that that user enters the employee info, starting with the name, last name, phone number, address, then the department.

But if there were 3 employees, first with a name starting with "a", second starting with a "d" and third starting with a "b", how would I sort the array alphabetically so that it would look like the first column of info under "a", then the second under "b" and the last under "d". I have gotten rid of some code I thought was unnecessary.

Any help would be appreciated.

Public Class StartForm

Dim FirstName As String
Dim LastName As String
Dim BothName As String
Dim FinalOutPut As String
Dim PhoneNumber As String
Dim Address As String
Dim Department As String
Dim SickDays As Integer
Dim AnnualDays As Integer

Dim arrayname(4, 1) As String
Dim countname As Integer = 0
Public Sub savebutton_Click(sender As Object, e As EventArgs) Handles savebutton.Click
    ' for names
    'REMINDER - y then x
    If countname = 0 Then
        arrayname(0, countname) = FirstNameBox.Text
        arrayname(1, countname) = LastNameBox.Text
        arrayname(2, countname) = PhoneBox.Text
        arrayname(3, countname) = AdressBox.Text
        arrayname(4, countname) = DepartBox.Text

        ReDim Preserve arrayname(4, countname)

    Else
        arrayname(0, countname) = FirstNameBox.Text
        arrayname(1, countname) = LastNameBox.Text
        arrayname(2, countname) = PhoneBox.Text
        arrayname(3, countname) = AdressBox.Text
        arrayname(4, countname) = DepartBox.Text

        ReDim Preserve arrayname(4, countname + 1)

    End If
End Sub

Private Sub displayButton_Click(sender As Object, e As EventArgs) Handles displaybutton.Click
    Dim time As String
    Dim Sickdays As String
    Dim Annualdays As String
    time = getButton()
    Sickdays = getSickDays()
    Annualdays = getAnnualDays()
    'test
    For Names = 0 To countname

        'Final output
        FinalOutPut = arrayname(0, Names) + " " + arrayname(1, Names) + " is " + time + " on time for work," + Environment.NewLine +
            "Sick Days: " + Sickdays + Environment.NewLine + "Annual Days:" + Annualdays + Environment.NewLine + "Address:" + arrayname(3, Names) +
            Environment.NewLine + "Phone Number:" +
            arrayname(2, Names) + Environment.NewLine + "Department: " + arrayname(4, Names)
        OutputTextBox.Text = FinalOutPut
        Names = Names + 1
    Next

End Sub

End Class

If it was me I would first create a class; for example: Person. Then I would put them into a ListOf(Person) . See below for a small example...

Person Class Example

 Option Strict On
 Option Explicit On

 Public Class Person

      Public Property FirstName As String
      Public Property LastName As String

 End Class

StartForm

 Option Strict On
 Option Explicit On

 Public Class StartForm

      Private lstPerson As New List(Of Person) 'Add to this list of Person

      Public Sub savebutton_Click(sender As Object, e As EventArgs) Handles savebutton.Click
         'Declare how many persons you want?
          Dim pOne, pTwo, pThree As New Person

         'Set their properties
          With pOne
           .FirstName = "Bobby"
           .LastName = "Walters"
          End With

          With pTwo
           .FirstName = "Shane"
           .LastName = "Waldo"
          End With

          With pThree
           .FirstName = "Harry"
           .LastName = "Waters"
          End With

          'Add them to the list now
           lstPerson.AddRange({pOne, pTwo, pThree})

          'Sort by last name/first name. You can change this...
          lstPerson = lstPerson.OrderBy(Function(x) x.LastName).ToList

      End Sub

 End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM