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
http://stackoverflow.com/questions/2038418/associatively-sorting-a-table-by-value-in-lua
http://stackoverflow.com/questions/15706270/sort-a-table-in-lua
http://lua.gts-stolberg.de/en/table.php#table.sort_(table_[,_comp])
hope it helps
Likes: jimlev
table.sort is a very powerful function and you would get what you want even with a multi-dimension table. To do a custom sort, you can write your own function or a simple comparison statement like a > b, for mutli-dimension sort, you can use a[1] > b[1] or the element that you want to sort on.
Hope that helps your problem.
Likes: jimlev
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Likes: jimlev
I 'm not familiar of the possibilities of giving a function in a method's parameters. So, I gonna give a try to the ar2rsawseen solution and, if it works (what I can't doubt) I hope that applying it will help me understand it
Likes: JuanZambrano
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.”
🤔