基本上,对于学校项目,我们必须使用冒泡排序对数据库进行排序(我知道效率非常低)。到目前为止,我通过从数据库创建一个数组然后对该数组进行排序来实现这一目标。我现在的问题是找出一种方法将这个已排序的数组放回数据库供用户查看。
这是我到目前为止的代码......
Dim dt As DataTable = Me.Patient_ListingDataSet.Tables(0)
'------ Creating Name Array ------'
listName.DataSource = Me.PatientListingBindingSource
listName.DisplayMember = "pName"
Dim Length As Integer = Me.listName.Items.Count - 1
i = 0
ReDim arrName(Length)
For Each row As DataRow In dt.Rows
arrName(i) = $"{(row("pName"))}"
i += 1
Next
'----- Bubble sorting Names (Z - A) -----'
Dim Length As Integer = Me.listName.Items.Count - 1
Dim swapped As Boolean = True
Dim bubbleTempName As String
While swapped = True
swapped = False
For Pass = 1 To Length
For i = 0 To Length - 1
If arrName(i) < arrName(i + 1) Then
bubbleTempName = arrName(i)
arrName(i) = arrName(i + 1)
arrName(i + 1) = bubbleTempName
swapped = True
End If
Next i
Next Pass
End While
我只是无法弄清楚如何将现在排序的数组返回到数据库中。提前致谢。 :)
答案 0 :(得分:0)
我找到了答案。我不得不使用for循环,然后添加一个新行,直到所有值都被添加进去。为了获得所有列,我必须为每个列创建一个数组,对每个列进行排序,然后最后添加已排序的数组再次找到合适的专栏。
以下是所有代码: Dim dt As DataTable = Me.Patient_ListingDataSet.Tables(0)'定义使用的数据表
'------ Creating Name Array ------'
listName.DataSource = Me.PatientListingBindingSource
listName.DisplayMember = "pName"
Dim Length As Integer = Me.listName.Items.Count - 1
i = 0
ReDim arrName(Length)
For Each row As DataRow In dt.Rows
arrName(i) = $"{(row("pName"))}"
i += 1
Next
'------ Creating Age Array ------'
listAge.DataSource = Me.PatientListingBindingSource
listAge.DisplayMember = "pAge"
i = 0
ReDim arrAge(Length)
For Each row As DataRow In dt.Rows
arrAge(i) = $"{(row("pAge"))}"
i += 1
Next
'------ Creating Weight Array ------'
listWeight.DataSource = Me.PatientListingBindingSource
listWeight.DisplayMember = "pWeight"
i = 0
ReDim arrWeight(Length)
For Each row As DataRow In dt.Rows
arrWeight(i) = $"{(row("pWeight"))}"
i += 1
Next
'------ Creating Height Array ------'
listHeight.DataSource = Me.PatientListingBindingSource
listHeight.DisplayMember = "pHeight"
i = 0
ReDim arrHeight(Length)
For Each row As DataRow In dt.Rows
arrHeight(i) = $"{(row("pHeight"))}"
i += 1
Next
'------ Creating BMI Array ------'
listBMI.DataSource = Me.PatientListingBindingSource
listBMI.DisplayMember = "pBMI"
i = 0
ReDim arrBMI(Length)
For Each row As DataRow In dt.Rows
arrBMI(i) = $"{(row("BMI"))}"
i += 1
Next
'------ Creating BMI Array ------'
listBMIClass.DataSource = Me.PatientListingBindingSource
listBMIClass.DisplayMember = "BMIClass"
i = 0
ReDim arrBMIClass(Length)
For Each row As DataRow In dt.Rows
arrBMIClass(i) = $"{(row("BMIClass"))}"
i += 1
Next
'----- Bubble sorting by Age (High - Low) -----'
'If the current index in Name array is smaller than the next index, the index will swap. Largest index will 'bubble' to the top
While swapped = True
swapped = False
For Pass = 1 To Length
For i = 0 To Length - 1
If arrName(i) > arrName(i + 1) Then
'Swapping the same index in the Name array
TempName = arrName(i)
arrName(i) = arrName(i + 1)
arrName(i + 1) = TempName
'Swapping the same index in the Age array
TempAge = arrAge(i)
arrAge(i) = arrAge(i + 1)
arrAge(i + 1) = TempAge
'Swapping the same index in the Weight array
TempWeight = arrWeight(i)
arrWeight(i) = arrWeight(i + 1)
arrWeight(i + 1) = TempWeight
'Swapping the same index in the Height array
TempHeight = arrHeight(i)
arrHeight(i) = arrHeight(i + 1)
arrHeight(i + 1) = TempHeight
'Swapping the same index in the BMI array
TempBMI = arrBMI(i)
arrBMI(i) = arrBMI(i + 1)
arrBMI(i + 1) = TempBMI
'Swapping the same index in the BMIClass array
TempBMIClass = arrBMIClass(i)
arrBMIClass(i) = arrBMIClass(i + 1)
arrBMIClass(i + 1) = TempBMIClass
swapped = True
End If
Next i
Next Pass
End While
Patient_ListingDataSet.Tables(0).Rows.Clear() 'Clears table for now sorted data
For Me.i = 0 To Length 'event occurs until the all indexes are added
'Creats a new row for each index being addad
Dim newRow As Patient_ListingDataSet.Patient_ListingRow
newRow = Me.Patient_ListingDataSet.Patient_Listing.NewRow()
'ensures arrays are added into corresponing columns of datatable
newRow.pName = arrName(i)
newRow.pAge = arrAge(i)
newRow.pWeight = arrWeight(i)
newRow.pHeight = arrHeight(i)
newRow.BMI = arrBMI(i)
newRow.BMIClass = arrBMIClass(i)
'the arrays are added to the created row
Me.Patient_ListingDataSet.Patient_Listing.Rows.Add(newRow)
Next
End Sub