'VB Moving several indexes at one time
Just wanted to ask a question on moving several indexes of an array at once while in a sorting algorithm.
Basically I'm using a bubblesort algorithm (in VB.NET) to sort a list of data which contains names, height, weight and age. Each name is essentially associated with each extra piece of data. This data is sent in from a txt file and then sorted using the algorithm. This file can also be edited to add new names or pieces of data.
Is there a way I am able to associate the pieces of data so that when the array is sorted the data stays with the names while the names have been sorted alphabetically?
Example of how txt file is set out and sorting method:
Unsorted:
Monty Reyes
28
1700
70.7
Kier Burke
45
1800
93.5
Sorted:
Kier Burke
45
1800
93.5
Monty Reyes
28
1700
70.7
My current code is just a simple bubblesort which sorts the entire array.
Private Sub btnNameSort_Click(sender As Object, e As EventArgs) Handles btnNameSort.Click
'turn the listbox items found in lstCurrentData to an array()
Dim Data() As String = lstCurrentData.Items.Cast(Of Object).Select(Function(o) lstCurrentData.GetItemText(o)).ToArray
Dim temp As String
For ii = 0 To Data.Length - 2
For i = 0 To Data.Length - 2
If Data(i) > Data(i + 1) Then
temp = Data(i)
Data(i) = Data(i + 1)
Data(i + 1) = temp
End If
Next
Next
For Each st In Data
lstSortArray.Items.Add(st)
Next
End Sub
If anyone has an idea on a way to create these associations within an array please inform me, I truly have no idea where to go from here. I tried to use Array.Copy but it got to complicated for me and I couldn't understand anything, I tried moving the new index in front of the name but then realised that would create an infinite loop. If there is a variable or something on this earth please just give me an idea of where to go. If anymore clarification is needed just ask. I'm still pretty new to VB so if I don't understand some terminology forgive me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
