1

I've created this program that will assemble a leaderboard array that will store scores from a game with the usernames beside the score. This leaderboard is constantly being updated everytime the game is played. These scores are appended into a text file, and the contents of the text file is entered into the leaderboard array. I want to sort this array so that the scores are descending, of course with the player name beside the corresponding score. The array looks something like this:

|User1, 9.0, 2, 0|User2, 8.0, 0, 1|User3, 8.5, 0, 1

So the format for this array goes like this:

|Username1, totalscore, wins, losses | Username2 , totalscore, wins, losses ect...

How would I sort this array so that the user with the highest score will appear first in the array and display it? I've heard that merge sort is easiest, but is this true? I appreciate any help!

6
  • so I am assuming you've already a string array where the first element is User1, 9.0, 2, 0 , second element User2, 8.0, 0, 1 and so forth? or is it a single string containing |User1, 9.0, 2, 0|User2, 8.0, 0, 1|User3, 8.5, 0, 1? Commented Mar 1, 2018 at 13:40
  • 1
    Can you use a list of tuples? It has an innate method for doing this and IMO is much easier to work with. Commented Mar 1, 2018 at 13:45
  • @Aominè its a single string. I was thinking of using the split function for the " | " that's between the losses and the username Commented Mar 1, 2018 at 13:49
  • @JacobH yes, i suppose i can use tuples Commented Mar 1, 2018 at 13:50
  • If you have a list you can do a lambda function like so fieldlist.OrderBy(Function(x) x.totalscore).ToList() or create a separate function call. Or the list.sort method. See here: stackoverflow.com/questions/1832684/… Some interesting stuff in some of the answers there that could be relevant to you. Commented Mar 1, 2018 at 13:53

1 Answer 1

2

You can split by the dilimeter "|" then utilise OrderByDescending like this:

Dim resultSet As String() = myString.Split("|"c) _
        .Where(Function(s) Not String.IsNullOrWhitespace(s)) _
        .OrderByDescending(
            Function(s) 
                Dim str as String = s.Substring(s.IndexOf(" ") + 1)
                Dim count As Integer = str.IndexOf(",")             
                Return Double.Parse(str.Substring(0, count))
            End Function) _
        .ToArray()

or if you want the elements into a List(Of String) then simply change:

Dim resultSet As String()

to:

Dim resultSet As List(Of String)

and then change the ToArray() call to ToList() in the pipeline above.

The final outcome of the above code is a String array of:

User1, 9.0, 2, 0
User3, 8.5, 0, 1
User2, 8.0, 0, 1

don't forget to use these imports:

Imports System.Linq 
Imports System.Collections.Generic
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply! Return Double.Parse(str.Substring(0, count)) threw an error - 'Input string was not in a correct format.' I changed the code around so the elements were a list(of string), but that didn't solve the problem. Sorry, i'm still a beginner at this
@user6297009 The issue is not about accumulating the elements into an array or a list. if your input is in the format |User1, 9.0, 2, 0|User2, 8.0, 0, 1|User3, 8.5, 0, 11 as you mentioned then it should work as expected, otherwise I'd recommend you step over the code with a debugger. you can try Return Double.Parse(str.Substring(0, count).Trim()) and see if it solves it, otherwise as mentioned you'll need to step over it with a debugger.
What is stepping with a debugger and how would you do it in my situation?
My format is the same as i mentioned previously. I tried Return Double.Parse(str.Substring(0, count).Trim()) but that didn't work...this is my code so far:
leaderlbl.Text = "Leader Board" Dim leaderboard As String For i As Integer = 0 To lines.Length - 1 leaderboard = leaderboard & " | " & lines(i) leaderlbl.Text = leaderlbl.Text & vbNewLine & lines(i).Split(New String() {","}, StringSplitOptions.None)(0).Split(",")(0) & " " & lines(i).Split(New String() {","}, StringSplitOptions.None)(1).Split(",")(0) Next MsgBox(leaderboard)
|

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.