1

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

1 Answer 1

1

highscores.Reverse() does not work because it returns reversed array not reversing existed array. To reverse array in place use Array.Reverse(highscores).

Also Array.Sort() work well on my example so most likely you have something strange in your highscores.txt. Can you give an example of this file.

Also resizing array on each iteration is bad idea because it is slow and wastes a lot of memory. You better use List. List can be easily sorted with linq like highscores.OrderBy(i => i) (c# syntax).

Sign up to request clarification or add additional context in comments.

3 Comments

highscores.Reverse() would work if OP would create a new array from it and assign that to highscores: highscores = highscores.Reverse().ToArray()
Thank you very much. My python background misled me to using highscores.reverse() rather than array.reverse(highscores) and I misformatted my highscores.txt file under the stress/confusion of debugging. Also, for future reference, would that list sort you mentioned sort two arrays like a dictionary like array.sort did?
Yes, you can sort two lists together with linq with zip (quite similar to python zip) or by combining them to dictionary. I will post full example then i have access to VS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.