Here's an example of the high-scores code in my BlitzMax project. It won't be copy-pastable, but it'll give you a good idea of how it works.
scores is an array containing all the scores in the table. sname is the array containing the 3-letter names to go along with each score. scorenum is the number of scores to record in the table, basescore is the lowest score, and scoreinc is how much each consecutive score increases by. The score at array index 0 is the highest score, and the last index is the lowest in the table. highscores.dat is the file I save the scores to, the first byte tell how many scores are in the table to prevent silly conflicts. Then all the scores and names in the array are recorded, in order.
writescores creates a new highscore file. readscores reads and puts in memory the scores saved in the highscore file. savescores saves the scores currently in memory to the file. addscore is the function I call whenever the player finishes a game to check if the player's score is higher than the lowest score on the table, and if it is it provides a light interface to enter their initials or w/e, and then shifts all the scores that will be below the newly placed score down to the next position in the arrat, then adds the new score and name at the appropriate place in the array.
Global scorenum%=20,basescore%=14000,scoreinc%=2000
Global scores%[scorenum],sname$[scorenum]
readscores
Function writescores()
Local fil:TStream=WriteFile("highscores.dat")
WriteByte fil,scorenum
For Local ws%=0 To scorenum-1
scores[ws]=(scorenum-1-ws)*scoreinc+basescore
WriteInt fil,scores[ws]
sname[ws]="---"
WriteString fil,Left(sname[ws],3)
Next
CloseFile fil
End Function
Function savescores()
Local fil:TStream=WriteFile("highscores.dat")
WriteByte fil,scorenum
For Local ws%=0 To scorenum-1
WriteInt fil,scores[ws]
WriteString fil,Left(sname[ws],3)
Next
CloseFile fil
End Function
Function readscores()
If FileType("highscores.dat")<>1 Then
writescores
Else
Local fil:TStream=ReadFile("highscores.dat")
If ReadByte(fil)<>scorenum Then
writescores
CloseFile fil
fil:TStream=ReadFile("highscores.dat")
ReadByte(fil)
EndIf
For Local ws%=0 To scorenum-1
scores[ws]=ReadInt(fil)
sname[ws]=Upper(ReadString(fil,3))
Next
CloseFile fil
EndIf
End Function
Function addscore(pts%)
SetColor 255,255,255
FlushKeys
readscores
If pts<scores[scorenum-1] Then Return
Local eslot%=1,strin$="AAA",req$="QWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()-=_+[]{};':~q,.<>/?\|`~~"
Repeat
Cls
drawstring "High score!",GraphicsWidth()/2,220,1
drawstring pts,GraphicsWidth()/2,240,1
drawstring "Please enter your name.",GraphicsWidth()/2,280,1
drawstring strin,GraphicsWidth()/2,310,1
Select eslot
Case 1 drawstring "_ ",GraphicsWidth()/2,312,1
Case 2 drawstring " _ ",GraphicsWidth()/2,312,1
Case 3 drawstring " _",GraphicsWidth()/2,312,1
End Select
Local char%=GetChar()
If char=key_enter Then
Local found%=scorenum-1
For Local insert%=0 To scorenum-1
If pts>scores[insert] And found=scorenum-1 Then
found=insert
EndIf
Next
For Local shift%=scorenum-1 To found Step -1
If shift+1<scorenum-1 Then
scores[shift+1]=scores[shift];sname[shift+1]=sname[shift]
EndIf
Next
scores[found]=pts;sname[found]=strin
savescores
FlushKeys
Return
ElseIf char<>0 Then
Local sc$=Upper(Chr(char))
If Instr(req,sc) Then
Select eslot
Case 1 strin=sc+Right(strin,2)
Case 2 strin=Left(strin,1)+sc+Right(strin,1)
Case 3 strin=Left(strin,2)+sc
End Select
eslot:+1
If eslot>3 Then eslot=1
EndIf
EndIf
Flip -1
If KeyDown(27) Or AppTerminate() Then End
Forever
End Function