I'm using the following code to read in a highscores file with the score written first and the player's name written to the next line, and then display the top 3 scores or fewer if there aren't 3 scores written to the file. A highscores display should have the scores sorted with the highest first so that's what this program tries to implement.
Array.sort() isn't doing a damn thing. In fact, the program's not even reversing the arrays like it should either. I've tested array.sort() this way with the names and scores arrays given explicitly and it works fine. I thought maybe it was still reading the data as string even though it's declared otherwise but it rounds off decimals when I change the array type to Integer so I'm pretty sure it's not that.
I have no idea why reading the data in from a file changes how it's sorted. My teacher didn't understand what was going on either. Does anyone know what's going on here?
Imports System.IO
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileLines() = File.ReadAllLines(Application.StartupPath & "/../../Resources/highscores.txt")
Dim highscores() As Double = {}
Dim names() As String = {}
'For some reason vb doesn't have a function to add items to arrays.
'Lists have this capability, but if I used a list I couldn't use array.sort
For i As Integer = 0 To fileLines.Length() - 1
If (i Mod 2 = 0) Then
'highscores.add(fileLines(i))
Array.Resize(highscores, highscores.Length + 1)
highscores(highscores.Length - 1) = fileLines(i)
Else
'names.add(fileLines(i))
Array.Resize(names, names.Length + 1)
names(names.Length - 1) = fileLines(i)
End If
Next
Array.Sort(highscores, names)
highscores.Reverse()
names.Reverse()
If highscores.Length() > 0 Then
Label1.Text = Str(highscores(0)) + " " + names(0)
End If
If highscores.Length() > 1 Then
Label2.Text = Str(highscores(1)) + " " + names(1)
End If
If highscores.Length() > 2 Then
Label3.Text = Str(highscores(2)) + " " + names(2)
End If
End Sub
End Class