Table Sort Problems

RandomCoder

Well-Known Member
Licensed User
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
Can you spot what I'm doing wrong please :confused:

Thanks,
RandomCoder
 

specci48

Well-Known Member
Licensed User
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?
 
Last edited:

RandomCoder

Well-Known Member
Licensed User
I've removed the , and then it errors.

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 :(

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
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.

Thanks,
RandomCoder
 

Erel

Administrator
Staff member
Licensed User
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).
 

RandomCoder

Well-Known Member
Licensed User
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.

Thanks for the help.

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
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's almost ready to be published now :sign0060:

Regards,
RandomCoder
 

specci48

Well-Known Member
Licensed User
Hi RandomCoder,

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:

specci48
 

RandomCoder

Well-Known Member
Licensed User
Well thanks once again fellas, we got there in the end!

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
Oh dear it still doesn't work

I guess I spoke too soon :(

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:confused:

Please help, I've wasted far too long on this already :sign0163:

Thanks,
RandomCoder
 

specci48

Well-Known Member
Licensed User
Hi RandomCoder,

I don't know why your solution doesn't work, but I have a working solution for you:
If you create the table header in the application, e.g.
B4X:
Sub App_Start
 SIP(False)
 WaitCursor(True)
[B] ScoresTable.AddCol(cNumber,"Score",60,False)
 ScoresTable.AddCol(cNumber,"Level",40,False)
 ScoresTable.AddCol(cString,"Date",70,False)[/B]
 LoadTable
 InitScoresForm
 ...
 ...
Sub LoadTable
 If FileExist("Hi-Scores.csv")=True Then
  ScoresTable.LoadCSV("Hi-Scores.csv",",",True,[B]False[/B])
  ScoresTable.TableSort("Score DESC, Level DESC")
 Else
 ...
and not automatically with LoadCsv the sorting problem is solved!

Maybe Erel is able to explain this behaviour :sign0163:

specci48
 

RandomCoder

Well-Known Member
Licensed User
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???

Regards,
RandomCoder
 

RandomCoder

Well-Known Member
Licensed User
THANK YOU, THANK YOU, THANK YOU.

I'll give it a try :sign0188:
 

RandomCoder

Well-Known Member
Licensed User
It's becoming a habit of mine :sign0161:
 

Cableguy

Expert
Licensed User
HUHUMMM!!

Hi Guys

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...
 
Top