This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
whatxDxDxD's profile picture

Sort Array

Started by whatxDxDxD, 22 September 2013 - 11:16 AM
whatxDxDxD #1
Posted 22 September 2013 - 01:16 PM

local list = {}
function setTable(...)
  local arguments = {select(1,...)}
  local player = arguments[1]
  if table.getn(arguments)==1 then
	list[player]={}
	list[player]["player"] = arguments[1]
	list[player]["deaths"] = 0
	list[player]["kills"] = 0
	list[player]["logins"] = 0
	list[player]["messages"] = 0
	list[player]["average"] = 0
  elseif table.getn(arguments)==6 then
	list[player]={}
	list[player]["player"] = arguments[1]
	list[player]["deaths"] = arguments[2]
	list[player]["kills"] = arguments[3]
	list[player]["logins"] = arguments[4]
	list[player]["messages"] = arguments[5]
	list[player]["average"] = arguments[6]
  else
	print("Error: Wrong parameter passed to 'setTable'!")
  end
  print("Player " ..arguments[1].. " successfully created.")
end

Well a while ago I asked about a similar problem already, but I still have problems with it and didn't want to post in a dead thread, so..
I have this array shown in the code above. Multiple players and their data are stored in there. Now I would like to be able to sort this array, sort by names, sort by kills etc.. I know there's table.sort() but i don't know if it's possible to use with more than one dimension and if so, how to write it exactly. I'm really having trouble with this :(/>
Thanks in advance,to everybody :)/>
Lyqyd #2
Posted 22 September 2013 - 01:25 PM
Uh, something like:


function sortByKills(x, y)
  return x.kills > y.kills
end

table.sort(list, sortByKills)

I think. If it's in the wrong order, use a less than in the function.
whatxDxDxD #3
Posted 22 September 2013 - 02:02 PM
first at all, thank you for the fast reply.
for testing I used this code.

function printer()
  for player in pairs(list) do
	print(list[player]["player"])
	print(list[player]["deaths"])
	print(list[player]["kills"])
	print(list[player]["logins"])
	print(list[player]["messages"])
        print(list[player]["average"])
  end
end

But in my tests it didn't matter if I used the sortfunction or not, the output was always the same. Is this because of pairs ? or did it probably not sort?
Lyqyd #4
Posted 22 September 2013 - 02:07 PM
Oh, yeah, you'll need to use numeric indices. So, don't key the table on player name, just use numeric indices, so that table.sort can swap them around.
whatxDxDxD #5
Posted 22 September 2013 - 03:21 PM
Ah, that's why :lol:/>. thank you :)/>

So instead of:
list["dede"]["kills"]
list["dede"]["deaths"]
list["dodo"]["player"]
it would be like:
list[1][3]
list[1][2]
list[2][1]
?

I use(d) several times code similar to this one:


function deathcount()
  while true do
	event,dead,killer,cause = os.pullEvent("chat_death")
	list[dead]["deaths"] = tonumber(list[dead]["deaths"])+1
	...

Here I would need to have a second table with the players and their related numbers?
So I could use a function to return me the right number, correct?



function deathcount()
  while true do
	event,dead,killer,cause = os.pullEvent("chat_death")
	number = giveNumber(dead)
	list[number][2] = tonumber(list[number][2])+1
	...

Sorry for asking so many (stupid) questions, I hope it's not annoying, sadly I'm not the best programmer.
Lyqyd #6
Posted 22 September 2013 - 03:31 PM
No, just list[1].kills, list[1].deaths, list[2].kills, list[2].deaths, etc.
whatxDxDxD #7
Posted 23 September 2013 - 11:55 AM
Thank you so much, finally worked :D/>.