-
I have a text file with 5 names, 5 numbers
ie
"Jim",444
"Mary",545
"Slim",234
"Rita",1234
"Me", 469
I now play a game as Harry and score 544
I want to sort the file (descending) and lose the least,
[Slim in this case] while adding Harry. I can sort one dimensional arrays but I haven't played with 2 if that were an option and I somehow think it could be.
"Rita", 1234
"Mary", 545
"Harry", 544
"Me", 469
"Jim", 444
-
If you want to sort this first time only, and with 5 items initially unsorted, you just could use the bubble sort you used to like ;) For multidimensioned arrays, i'm not sure what you mean, if you have a variant array with strings and numerics, or a string array with numeric strings for the results. It would be better to use a udt intead since you can handle both the strings and numerics. All sort algoritms have a part where it swaps two items, instead of just swaping the numbers, you swap the names at the same time with the same indexes.
If you save the file and have it sorted next time, you could implement a binary search which will stick in the next name at next position. to remove the last item, you just redim preserve the array. For multidimensional arrays you have to use the last dimension as the dynamic one.
-
<?>
kedaman:
Thanks, but being as I'm too lazy to research the subject, I will opt out for the database and just sort the recordset by score.
Thanks again,
Wayne
-
<?>
In case anyone is interested as there were a few views.
Code:
If Dir(App.Path & "/db1.mdb") <> "" Then
Dim db As Database
Data1.DatabaseName = App.Path & "/db1.mdb"
Set db = Workspaces(0).OpenDatabase(Data1.DatabaseName)
Dim sql As String
sql = "select * from table1 order by field2 desc"
Set Data1.Recordset = db.OpenRecordset(sql)
'add the latest player and refresh recordset
Data1.Recordset.AddNew
Data1.Recordset!field1 = NameOfPlayer(1).Name
Data1.Recordset!field2 = NameOfPlayer(1).Score
Data1.Recordset.Update
Data1.Refresh
Dim i As Integer
'get rid of surplus
Data1.Recordset.MoveFirst
While Data1.Recordset.RecordCount > 5
Data1.Recordset.MoveLast
Data1.Recordset.Delete
Wend
'start at front end and list the five top players
Data1.Recordset.MoveFirst
frmTop.List1.Clear
frmTop.List1.FontSize = 10
frmTop.List.FontBold = True
frmTop.List1.AddItem "Top Five Scorers"
frmTop.List1.AddItem ""
While Not Data1.Recordset.EOF
frmTop.List1.AddItem Data1.Recordset!field2 & " " & Data1.Recordset!field1
Data1.Recordset.MoveNext
Wend
frmTop.Visible = True
FrmMain.Visible = False
End If
-
dont use data control
in my opinion data control is for those that are REALLY lazy :)
(and i dont think you are one of THOSE since you help a lot of people out)
use pure ADO Code instead of data control
and from the looks of it
your using DAO
ADO is better in my opinion and many others