I'm trying to create a generic hi-scores table for one of my game apps and also so that I can add it to the code samples for others to use
It's almost finished now but when I add a new score to the table and then sort the scores I find that the rows in the table are being separated instead of being kept together. Here's the section of code that adds the new score to the table and then attempts to sort it...
B4X:
Sub UpdateTable(Score,Level)
ScoresTable.Cell("Score",9)=Score
ScoresTable.Cell("Level",9)=Level
ScoresTable.Cell("Date",9)=Date(Now)
ScoresTable.TableSort ("Score DESC, Level DESC")
ScoresForm.Show
End Sub
Hmm ... I don't see any error in this code sample.
What happens if you try this:
B4X:
Sub UpdateTable(Score,Level)
ScoresTable.RemoveRow(9)
ScoresTable.AddRow(Score, Level, Date(now))
ScoresTable.TableSort ("Score DESC, Level DESC")
ScoresForm.Show
End Sub
Or what happens, if you change the variable names so that they are different to the column names?
I've also tried to sort using only the Score column but it still ends up with the data in the rows getting mixed up. This is probably really simply to solve but its got me beat
No, both columns are set as cStrings because when the table is first started I display blanks but if I set the columns as cNumber then 0's are displayed which looks a little unsightly.
I don't understand why the data in the rows would get mixed up using either cString or cNumber, surely the data in each row should stay constant, just the position of the row should change in relation to the data that is being sorted.
TableSort doesn't sort the table once.
It keeps the table sorted all the time.
Now, when you write:
ScoresTable.Cell("Score",9)=Score
Row 9 will change depending on the score value.
The next update (level update) will update another row.
As specci49 wrote you should add a new row at once and you don't need to use TableSort each time (it doesn't do anything).
When I replied earlier I hadn't seen Specci48's reply, we must have both been replying at the same time. I see now what I need to do, instead of updating the values in the cells I should remove the row and then add a new row with the updated data. I'll give it a go and let you know how I do.
Thank you, removing the last row then adding a new row has done the trick. I hadn't properly understood that TableSort kept the table sorted even after new data is entered.
it doesn't matter that you missed my post. The same happend to me not long ago. Now I reload every thread I had posted to after sending, just to be sure I hadn't missed something.
And in this special case it was a "plus" to miss it because...
... if you had read my post, you may have changed your code which has solved the problem.
... now Erel has referenced to my post (who the fu.. ist specci49 ??? ) and you changed your code which has solved the problem. But additionally now we all know how TableSort is really working since Erel explained, that a table is sorted all the time. :sign0095:
I was just about ready to post my little app in the "Code and Samples" section when I found that occasionally it fails to sort the scores correctly :sign0148:
Attached is the faulty albeit nearly complete app. I've disabled the save to CSV so that it won't overwrite the problematic data. I've tried sorting using only the score and not the level but still no joy. At first I was assigning the values as strings in the table (just so that zero's didn't show when no score was available). Now I've changed it to cNumbers and modified the labels to display blanks if the table value is zero but this also hasn't worked.
If you delete the table then all appears to work OK until the next time the app is run, this is when I appear to see the problem.
Is this a bug in .NET or just a glitch in my code
Please help, I've wasted far too long on this already :sign0163:
OK, now I've tried to work out whats happening a little further and it looks like the values in the table are having zero's appended to them (i.e. the score of 40 is actually being read as 40000). I've opened the csv file using excel and that displays it as 40 so I'm still struggling to understand the reason why???
RandomCoder, since you fromated the values format from string to number, you should also hard format the number itself...maybe the extra zerros that are beeing read, are beeing aded at runtime by a wrong number format....
Anyway hope Specci48 solution works for you...