[英]How to sort a 2 dimensional array alphabetically
我正在嘗試學習Visual Basic,因此我決定創建一個簡單的程序,讓該用戶輸入員工信息,從姓名,姓氏,電話號碼,地址開始,然后是部門。
但是,如果有3名員工,第一名的名稱以“ a”開頭,第二名的名稱以“ d”開頭,第三名的名稱以“ b”開頭,那么我將如何按字母順序對數組進行排序,使其看起來像是info在“ 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
如果是我,我將首先創建一個班級。 例如:人。 然后,我將它們放入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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.