Thread Tools Display Modes
04-22-15, 04:43 PM   #1
tobias1919
A Deviate Faerie Dragon
Join Date: Mar 2015
Posts: 13
Possible to sort a table by a 2nd index?

I will try explaining this as good as i can...
Trying to build an addon to collect random roll results, and list them nicely with name, roll size, if roll is valid (1-100) and then sort the table, so highest roll shows at top.

I first thought collecting the rolls in a table:

Lua Code:
  1. local RollCollector = {}
  2. function RollMaster:CHAT_MSG_SYSTEM(event, ...)
  3.     local arg1 = select(1, ...)
  4.     if arg1 then
  5.         local name, roll, minRoll, maxRoll = arg1:match("^(.+) rolls (%d+) %((%d+)%-(%d+)%)$")
  6.         for i = 1, 20 do
  7.             if RollCollector[i] and RollCollector[i][1] == name then
  8.                 print(name.." rolled twice")
  9.                 roll = nil
  10.             end
  11.         end
  12.         if roll then
  13.             table.insert(RollCollector, 1, 0)
  14.             RollCollector[1] = {name, roll, minRoll, maxRoll}
  15.         end
  16.     end
  17. end

However, i'm not sure about how to sort a table like that (if even possible).

Then i thought about doing like 4 tables:
Lua Code:
  1. local RollCollector = {
  2.     name = {},
  3.     roll = {},
  4.     minRoll = {},
  5.     maxRoll{},
  6. }

and saving each arguement to its own table... But when running the table.sort method like this, it will only sort the rolls, which would remove the link between name and roll...

Anyone got a solution for this?


(Hope you understand the question, beside my rusty english!!)

Thanks alot on advance!
  Reply With Quote
04-22-15, 05:03 PM   #2
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
I don't think you need to sort the table, just keep it ordered as you add data
Code:
if maxroll<roll then
	maxroll = roll
end
if RollCollector[roll]==nil then
	RollCollector[roll] = {} --more than one person can roll the same number that is why the table
end
if minRoll==1 and maxRoll==100 then
	table.insert(RollCollector[roll],name)
else
	print(name,"is cheating")
end
to present

Code:
for i=100,1,-1 do
	if RollCollector[i] then
		for k,v in ipairs(RollCollector[i]) do
			print(i,v)
		end
	end
end
if you just want the highest then you just access RollCollector[maxroll] (which is a list of people that rolled the max, most of time just one person)

this is dry coded but I hope you get the idea.

Last edited by Banknorris : 04-22-15 at 05:15 PM.
  Reply With Quote
04-22-15, 06:22 PM   #3
tobias1919
A Deviate Faerie Dragon
Join Date: Mar 2015
Posts: 13
Thanks alot

Made it work with very few modifications (Ofcourse i can't remember what i changed by now :S )

Altho, thank you again...!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Possible to sort a table by a 2nd index?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off