Results 1 to 3 of 3

Thread: [RESOLVED] Sorting array with 2 columns

  1. #1

    Thread Starter
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Resolved [RESOLVED] Sorting array with 2 columns

    Hello, in my game I have the high scores. They are stored as the Players name and then the score, like;
    Kevin,2548
    Peter,36982

    When someone ends the game, I would like loop through the ini file used to store the high scores and read them into an array or listbox, the latter so I can make sure its working).

    So i would have the first 10 fields filled with the data and then i will make the 11th the new entry. I would like to sort them by the score and then name and then write the first 10 back into the file, dropping the 11th since it would then be the last one.

    using either an array or a listbox would be fine.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Sorting array with 2 columns

    try something like this:

    you need an invisible listbox with sorted = true

    VB Code:
    1. Private Sub Command1_Click()
    2.     GenHiScores "balh", "600"
    3. End Sub
    4.  
    5. Private Sub GenHiScores(ByVal sNewName As String, ByVal sNewScore As String)
    6.     Dim sName As String, sScore As String, sParts() As String
    7.     Dim FF As Integer, N As Long
    8.     FF = FreeFile
    9.     ' Load previous scores
    10.     Open App.Path & "\scores.txt" For Input As #FF
    11.         Do Until EOF(FF)
    12.             Input #1, sName, sScore
    13.             List1.AddItem Format$(Trim$(sScore), String(12, "0")) & vbTab & sName
    14.         Loop
    15.     Close #FF
    16.    
    17.     ' Add the new score
    18.     List1.AddItem Format$(Trim$(sNewScore), String(12, "0")) & vbTab & sNewName
    19.    
    20.     FF = FreeFile
    21.     ' Save the list
    22.     Open App.Path & "\scores.txt" For Output As #FF
    23.         For N = List1.ListCount - 1 To IIf(List1.ListCount - 10 < 0, 0, List1.ListCount - 10) Step -1
    24.             sParts = Split(List1.List(N), vbTab)
    25.             Print #1, sParts(1) & "," & Val(sParts(0))
    26.         Next N
    27.     Close #FF
    28. End Sub

  3. #3

    Thread Starter
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Sorting array with 2 columns

    bushmobile, thanks, it works perfect. Now I can cut out another form for the game and have it display the scores on one form.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width