[英]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. 我正在尝试学习Visual Basic,因此我决定创建一个简单的程序,让该用户输入员工信息,从姓名,姓氏,电话号码,地址开始,然后是部门。
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". 但是,如果有3名员工,第一名的名称以“ a”开头,第二名的名称以“ d”开头,第三名的名称以“ b”开头,那么我将如何按字母顺序对数组进行排序,使其看起来像是info在“ a”下,然后第二个在“ b”下,最后一个在“ 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)
. 然后,我将它们放入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 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.