如何按字母顺序对二维数组进行排序



我正在尝试学习Visual Basic,所以我决定创建一个简单的程序,该用户输入员工信息,从姓名,姓氏,电话号码,地址开始,然后是部门。

但是,如果有 3 名员工,第一个名字以"a"开头,第二个以"d"开头,第三个以"b

"开头,我将如何按字母顺序对数组进行排序,使其看起来像"a"下的第一列信息,然后是"b"下的第一列信息,最后一列在"d"下。我已经摆脱了一些我认为不必要的代码。

任何帮助将不胜感激。

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

如果是我,我会先创建一个类;例如:Person。然后我会把它们放进一个ListOf(Person).请参阅下面的一个小示例...

人员类示例

 Option Strict On
 Option Explicit On
 Public Class Person
      Public Property FirstName As String
      Public Property LastName As String
 End Class

开始窗体

 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

最新更新