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.
CodePudding user response:
If you make a class for the data (Objects and classes in Visual Basic), you can use LINQ (Introduction to LINQ in Visual Basic) to sort it on whichever property you like.
To demonstrate, I made this as a Console App for simplicity, and you will have to write the part to read in and parse the data from the file:
Module Module1
Public Class Person
Property Name As String
Property Age As Integer
Property Height As Integer
Property Weight As Double
Public Overrides Function ToString() As String
Return String.Format("({0}: {1} years old, {2} mm, {3} kg)", Name, Age, Height, Weight)
End Function
End Class
Public Sub ShowPeople(people As IEnumerable(Of Person))
Console.WriteLine(String.Join(vbCrLf, people))
End Sub
Public Sub BubbleSortPeopleByName(ByRef people As List(Of Person))
For i = 0 To people.Count - 2
Dim doneSwap = False
For j = i 1 To people.Count - 1
If people(i).Name > people(j).Name Then
Dim tmp = people(j)
people(j) = people(i)
people(i) = tmp
doneSwap = True
End If
Next
If Not doneSwap Then Return
Next
End Sub
Sub Main()
Dim people As New List(Of Person)
'TODO: Load the data from a file.
people.Add(New Person With {.Name = "Monty Reyes", .Age = 28, .Height = 1700, .Weight = 70.7})
people.Add(New Person With {.Name = "Kier Burke", .Age = 45, .Height = 1800, .Weight = 93.5})
Console.WriteLine("Unsorted:-")
ShowPeople(people)
BubbleSortPeopleByName(people)
Console.WriteLine("Sorted:-")
ShowPeople(people)
Console.ReadLine()
End Sub
End Module
Outputs:
Unsorted:-
(Monty Reyes: 28 years old, 1700 mm, 70.7 kg)
(Kier Burke: 45 years old, 1800 mm, 93.5 kg)
Sorted:-
(Kier Burke: 45 years old, 1800 mm, 93.5 kg)
(Monty Reyes: 28 years old, 1700 mm, 70.7 kg)
The Person class has its own .ToString() method to make it easy to show an instance of Person as a string.
The ShowPeople method has people As IEnumerable(Of Person) as its parameter instead of a List(Of Person) because a List is an IEnumerable but not vice versa, and it makes it more versatile.
