Good day, to those reading this.
I do hope that what follows holds the attention of a helpful-patient-someone(s)
that may assist me in what must truly be trivial matters to most.
Thank you in advance for reading this.
Using the table.sort() function, I am successfully ordering a topScores{table} by a certain game time, in seconds.
topScores[1].sec = 38 SECONDS
Now, in the case where two or more table entries (top scores) may have the same game time,
I would like to further sort those specific entires by the “game points”.
topScores[1].pts = 54 POINTS
Of course, still under the ‘sort-restraints’ of the first table.sort() of the ‘sec’ KEY
ex.
topScores = { UNSORTED
{ pts = 54, sec = 38 }, THIS SHOULD BE IN 2ND PLACE, BASED ON ‘sec’ AND ‘pts’
{ pts = 101, sec = 46 } THIS SHOULD BE IN 3RD PLACE, BASED ON ‘sec’
{ pts = 99, sec = 38 }, THIS SHOULD BE IN 1ST PLACE, BASED ON ‘sec’ AND ‘pts’
}
local holdSortedTable = {}
table.sort(topScores, function(a,b) return a.sec < b.sec end)
for i=1,5,1 do
holdSortedTable[i] = topScores[i]
end
topScores = holdSortedTable
topScores = { SOME WHAT SORTED
{ pts = 54, sec = 38 }, THESE NEED TO SWAP ie. 54 pts is less than 99 pts
{ pts = 99, sec = 38 }, THESE NEED TO SWAP ie. 99 pts is greater than 54 pts
{ pts = 101,sec = 46 }
}
NOW I NEED TO SORT BY: ‘PTS’ POINTS
NOTE: (only those entries with the same “sec” value, 38 in this case)
Question 1:
Continuing with the code above, how would that additional sub-sorting of ‘pts’ be coded?
Question 2:
And lastly, when it comes time to adding a new score, how do I iterate through the topScores{} table,
such that, it looks for duplicate “sec” KEY VALUES?;
so that a new score (possibly having the same game time as an existing score)
is properly INSERTED, or ignored, based on the ‘points’ value?
newScore = { pts = 55, sec = 38 }
topScores = {
{ pts = 99, sec = 38 },
{ pts = 54, sec = 38 }, newScore INSERTED HERE { pts = 55, sec = 38 }
{ pts = 101, sec = 46 } “BUMPED OFF” AND 2nd place becomes 3rd place { pts = 54, sec = 38 }
}
Continuing with the code above, how would that additional “COMPARING” of ‘sec’ and ‘sec’ and of ‘pts’ and ‘pts’ with a new table entry be coded?
Thank you, again.
I appreciate your time, and any help you may have.
Chris