Iwasawa Posted December 7, 2013 Share Posted December 7, 2013 Help me please with table sorting. I want to write a custom race event script for play. The idea is clients send their progress like 23/40cp to the server, that adds them into a table, the table gets sorted and then the rankings will shown. Please help me how could i idendify the players and their rank. the table in server would look like racers = {somebody = 34, idk = 12, racer20 = 20} then sort racers table and write current ranks. But you can tell me other variations if you know easier ones. Link to comment
Iwasawa Posted December 7, 2013 Author Share Posted December 7, 2013 Do you think im moron? I tried, it does not work in this case. Link to comment
Renkon Posted December 9, 2013 Share Posted December 9, 2013 You can try by bubble sorting. Let's say you got a table called racers what holds playerVar = checkpointNumber. So, moving into code: racers = { somebody = 10, else = 17, pete = 5, randomPlayerVar = 25, } for i = 1, #racers do for j = #racers, 1, -1 do -- not sure if this line is ok though if racers[i] < racers[j] then local temp = racers[i] racers[i] = racers[j] racers[j] = temp end end end Link to comment
ixjf Posted December 9, 2013 Share Posted December 9, 2013 Do you think im moron? I tried, it does not work in this case. It does not, because non-numerical indexed tables don't have an order. However, you can organise your table like this: local racers = { { player = somePlayer, rank = 20 }, -- "somePlayer" would be a player element { player = anotherPlayer, rank = 5 }, -- "anotherPlayer" would be a player element { player = me, rank = 30 }, -- "me" would be a player element { player = cheater, rank = 140 } } -- "cheater" would be a player element You can then sort this table: table.sort (racers, function (a, b) return a.rank > b.rank end) Link to comment
Desaster Posted December 9, 2013 Share Posted December 9, 2013 this might help you viewtopic.php?f=91&t=68164 Link to comment
ixjf Posted December 9, 2013 Share Posted December 9, 2013 That topic has nothing to do with what is being asked here. Link to comment
Iwasawa Posted December 30, 2013 Author Share Posted December 30, 2013 Thanks btw, I'll try it. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now