Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
sort a multi-dimensionnal table — Gideros Forum

sort a multi-dimensionnal table

jimlevjimlev Member
edited August 2014 in Code snippets
Hi !
Before I launch myself in an too-big-for-me bunch of code, it would be nice if someone can confirm I'm on the right way.

Here it is :
I have a multi-dim table like that one :
-- name of the player / score / energy / does he have a bonus?
playerRanking = {{"john", 12, 0, false},{"charles", 17, 5, false},{"tony", 3, 3, true},{"marcello", 17, 11, false},{"rebecca", 12, 0, true}}

my goal is to sort that table by the players score. In case of equality, the energy parameter should be used to decide. And then, if players have or not a bonus.

the result should look like :
playerRanking = {{"marcello", 17, 11, false},{"charles", 17, 5, false},{"rebecca", 12, 0, true},{"john", 12, 0, false},{"tony", 3, 3, true}}


The way I'm going to approach the problem is :
1. copy all the score values in a simple table and sort it with the table.sort method.
2. with a loop, check in my playerRanking table if there is equality on the highest score
3. if answer is no, I copy that player to a temp table at the first index
4. if yes, I compare the other parameters to decide. And then, I copy these players at the first and second indexes
5. etc. on the next values from my sorted list

Am I on the right way. Is there a more trivial method to do that ?

thx !
My meditation plan :
SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
Totebo: “ Best quote ever.”
🤔

Comments

Sign In or Register to comment.