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!

  • 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`? – Ousmane D. Mar 01 '18 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. – Jacob H Mar 01 '18 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 – user6297009 Mar 01 '18 at 13:49
  • @JacobH yes, i suppose i can use tuples – user6297009 Mar 01 '18 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: https://stackoverflow.com/questions/1832684/c-sharp-sort-and-orderby-comparison Some interesting stuff in some of the answers there that could be relevant to you. – Jacob H Mar 01 '18 at 13:53
  • @JacobH I had never thought of using tuples in sorting. Found a timed project on Code Project and Tuples beat Linq and Data Tables. – Mary Mar 02 '18 at 06:32

1 Answers1

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
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 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 Mar 02 '18 at 10:11
  • @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. – Ousmane D. Mar 02 '18 at 13:33
  • What is stepping with a debugger and how would you do it in my situation? – user6297009 Mar 02 '18 at 19:54
  • 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: – user6297009 Mar 02 '18 at 19:56
  • `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)` – user6297009 Mar 02 '18 at 19:56
  • `Dim resultSet As String() = leaderboard.Split("|"c) _ .Where(Function(g) Not String.IsNullOrWhiteSpace(g)) _ .OrderByDescending( Function(g) Dim str As String = s.Substring(s.IndexOf(" ") + 1) Dim count As Integer = str.IndexOf(",") Return Double.Parse(str.Substring(0, count).Trim()) End Function) _ .ToArray()` Sorry its all a bit long – user6297009 Mar 02 '18 at 19:56