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

Sorting a table

Started by whatxDxDxD, 07 July 2013 - 11:34 AM
whatxDxDxD #1
Posted 07 July 2013 - 01:34 PM
Well the title said pretty much everything.
First off, I'm not a good programmer, I'm pretty sure there are better and cleaner ways to do this.
Sorry if the code is a little bit of a mess, did my best. :)/>
And I don't know if I used the "in pairs" correctly, but for now the program works so far.

So it would be nice if I could press a button on the screen and sort the table depending on which button I pressed and of course reprint the sorted table on the monitor. For the buttons I'd use a Button API and parallel.wait ForAnd() to run both at the same time. But for now I've got a problem with writing a function to sort the table. For example sorting depending on kills etc. I looked it up but I didn't get it.



So here's my actual code, it's working, but without the sorting function(s).
Thanks in advance. :)/>
Spoiler

--Score Board by Southp0le
--adjusted for 6x8 Advanced Monitor

-----------
--Options--
-----------
mon = peripheral.wrap("top")
chat = peripheral.wrap("bottom")
mon.setTextColor(colors.black)
mon.setBackgroundColor(colors.lightBlue)
mon.setTextScale(2)

-------------
--Functions--
-------------
--creates table with all infomation
local list = {}
function setTable(player,deaths,kills)
  list[player]={}
  list[player]["player"] = player
  list[player]["deaths"] = deaths
  list[player]["kills"] = kills
end

--saves list in file "archiv"
function save()
  local file = fs.open("archiv","w")
  file.write(textutils.serialize(list))
  file.close()
end

--loads file "archiv" and returns the list
function load()
  local file = fs.open("archiv","r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end

--checks if theres a file "archiv", if not creates one
function check()
  if fs.exists("archiv")==false then
	save()
	print("Created a file")
  else
	print("Everything fine, file exists already")
  end
end

--adds a new player to the list
function addPlayer(player)
	setTable(player,0,0)
end

--creates labels
function label(xpos,ypos, text)
  mon.setCursorPos(xpos, ypos)
  mon.write(text)
end

--prints all the labels on the monitor
function screen()
  label(2,2,"Player:")
  label(21,2,"Deaths:")
  label(33,2,"Kills:")
  label(30,20,"by Southp0le")
end

--prints the list on the monitor
--"tostring" to get rid of ".0"
function printList()
  local x=4
  mon.clear()
  for player in pairs(list) do
	mon.setCursorPos(3,x)
	mon.write(player)
	mon.setCursorPos(25,x)
	mon.write(tostring(list[player]["deaths"]))
	mon.setCursorPos(36,x)
	mon.write(tostring(list[player]["kills"]))
	x=x+1
  end
end

--waits for death message
--increase deathnumber of dead player by 1
--if theres a killer,he will get +1 kill
--if theres no table of the dead player/killer -> adds one
function score()
  while true do
	mon.clear()
	printList()
	screen()
	event,dead,killer,cause=os.pullEvent("chat_death")
	if list[dead] == nil then
	  addPlayer(dead)
	end
	list[dead]["deaths"]=tonumber(list[dead]["deaths"])+1
	if killer~=nil then
	  if list[killer] == nil then
		addPlayer(killer)
	  end
	  list[killer]["kills"]=tonumber(list[killer]["kills"])+1
	end
	save()
  end
end

--still working
  --function sortDeath() end
  --function sortKill() end
  --function sortName() end


-----------
--Process--
-----------
check()
list = load()
score()
Grim Reaper #2
Posted 07 July 2013 - 02:24 PM
You'll need to specify exactly what you mean by sorting. In terms of numbers, did you want ascending (lowest to greatest) or descending (greatest to lowest)? With strings you need to know if you want to sort by length or alphabetical or any other way you can think of to do that. Sorting itself isn't terribly difficult and its pretty easy to find methods online which explain how the sort is performed.

If you're just sorting numbers, look into merge sort.
theoriginalbit #3
Posted 07 July 2013 - 02:29 PM
Sorting itself isn't terribly difficult and its pretty easy to find methods online which explain how the sort is performed.


If you're just sorting numbers, look into merge sort.
Why reinvent the wheel, just pass a sort function pointer to the table.sort function, dependent on whichever is the current `filter`.