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

时间:2014-11-25 02:19:22

标签: arrays vb.net sorting dynamic-arrays

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

但是,如果有3名员工,首先是名字以&#34开头; 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

1 个答案:

答案 0 :(得分:1)

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

人员类示例

 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