|
-
Apr 28th, 2006, 12:52 PM
#1
Thread Starter
Fanatic Member
[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.
-
Apr 28th, 2006, 08:22 PM
#2
Re: Sorting array with 2 columns
try something like this:
you need an invisible listbox with sorted = true
VB Code:
Private Sub Command1_Click()
GenHiScores "balh", "600"
End Sub
Private Sub GenHiScores(ByVal sNewName As String, ByVal sNewScore As String)
Dim sName As String, sScore As String, sParts() As String
Dim FF As Integer, N As Long
FF = FreeFile
' Load previous scores
Open App.Path & "\scores.txt" For Input As #FF
Do Until EOF(FF)
Input #1, sName, sScore
List1.AddItem Format$(Trim$(sScore), String(12, "0")) & vbTab & sName
Loop
Close #FF
' Add the new score
List1.AddItem Format$(Trim$(sNewScore), String(12, "0")) & vbTab & sNewName
FF = FreeFile
' Save the list
Open App.Path & "\scores.txt" For Output As #FF
For N = List1.ListCount - 1 To IIf(List1.ListCount - 10 < 0, 0, List1.ListCount - 10) Step -1
sParts = Split(List1.List(N), vbTab)
Print #1, sParts(1) & "," & Val(sParts(0))
Next N
Close #FF
End Sub
-
May 1st, 2006, 08:29 AM
#3
Thread Starter
Fanatic Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|