0

I am trying to make a leader board that puts highest scores at the top or left with this layout

99 - james,
90 - will,
80 - dan,
70 - phil,
60 - kevin,
570 - jim,
50 - ben,
40 - david,
30 - jose,
220 - peter,
20 - tony,
10 - nick,

The .sort command doesn't work for numbers 3 digits and up i have a list that i am tying to sort but it is not working.

This is what i am currently working with.

leaderboard.Sort() leaderboard.Reverse()

It does sort numbers under 100 perfectly well this is the only issue i have.

Dim leaderboard As New List(Of String)

    Using Reader As New StreamReader("C:\Users\1111\OneDrive\Documents\Leaderboard.txt")
        While Reader.EndOfStream = False
            leaderboard.Add(Reader.ReadLine())
        End While
    End Using

    leaderboard.Sort()
    leaderboard.Reverse()
Mary
  • 14,926
  • 3
  • 18
  • 27
will
  • 1
  • 1
  • 2
    Are you sorting numbers or strings? Also, what is `leaderboard` made of? ([Edit](https://stackoverflow.com/posts/64829744/edit) your question to add these details) – Jimi Nov 14 '20 at 00:37
  • ...and make sure you post a [repro]. – 41686d6564 stands w. Palestine Nov 14 '20 at 00:41
  • @Jimi the variable is a string but .sort still works perfectly up to 2 digits also the leaderboard is made if the scores(the numbers) and usenames(names) you know a leaderboard – will Nov 14 '20 at 00:50
  • @will _"but .sort still works perfectly up to 2 digits"_ No, [it does not](https://rextester.com/BPIVR45672). Sorting strings doesn't work the same as sorting numbers. – 41686d6564 stands w. Palestine Nov 14 '20 at 00:54
  • cheers @41686d6564 that really helped thanks to your comment i no longer have any issues – will Nov 14 '20 at 00:58
  • @will I'm not sure if you're being sarcastic but personally, I'd be thankful if someone pointed out a misconception that I had. Maybe that's just me. Anyway, I was trying to find you a good duplicate. I couldn't find a _good_ one for VB but [this post](https://stackoverflow.com/q/6396378/8967612) is about C# and it has several great solutions that should be easily convertible to VB. – 41686d6564 stands w. Palestine Nov 14 '20 at 01:01
  • @41686d6564 i was being incredibly scarcastic i have tried it many times and you your self can try it it works fine up to two digits just put a bunch of random 2 digit numbers in a notepad file use the code i put up above obviously change the 1111 for your user account and output you will see it works – will Nov 14 '20 at 01:07
  • @will Apparently, you didn't click on the link in my second comment (that one that says "it does not"). It _will_ work fine if all your numbers consist of two digits but it _could_ fail if you have a mix of one-digit and two-digit numbers (which is what I think you mean by "up to two digits) as demonstrated in the link above. – 41686d6564 stands w. Palestine Nov 14 '20 at 01:10

1 Answers1

2

First I made a Structure as a template to hold your data. It has 2 properties. One to hold the Score and one to hold the name.

Private Structure Leader
    Public Property Score As Integer
    Public Property Name As String
End Structure

The code starts out by creating a new list of Leader (the name of the structure). I used the File class from System.IO (you will need to add this to the list of Imports at the top of the code file). .ReadAllLines returns an array of strings, each element is a single line from the text file.

Then we loop through each line, splitting the line by the hyphen. This will give your an array of strings with 2 elements. Before you try to convert the the first element to an Integer be sure to trim off any spaces. The second element of the array will contain the name and need to be trimmed. I also replaced the comma with an empty string.

Finally, a bit of Linq magic orders the list in descending order by score into another list. Function(lead) is a function that takes each item in the original list and tests its Score property. I called .ToList at the end so orderedLeader could be display in a DataGridView.

   Private Sub OPCode()
    Dim leaderboard As New List(Of Leader)
    Dim lines = File.ReadAllLines("leaderboard.txt")
    For Each line In lines
        Dim splitLine = line.Split("-"c)
        Dim sc = CInt(splitLine(0).Trim)
        Dim nm = splitLine(1).Trim.Replace(",", "")
        leaderboard.Add(New Leader With {.Score = sc, .Name = nm})
    Next
    Dim orderedLeaderbord = leaderboard.OrderByDescending(Function(lead) lead.Score).ToList

    DataGridView1.DataSource = orderedLeaderbord
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27