Match make rating(MMR) and team balancing with multiple tables

Hello! I’m working on trying to group up players into two teams of 5 based on their ratings. The goal is to take all player’s MMR values which is the 2nd value in each array in the local variable in QueueTable. Then out of the values given to find the fairest match possible by having teams MMR sum as close as possible.

local QueueTable = {
	{"P1userId", 300};
	{"P2userId", 240};
	{"P3userId", 275};
	{"P4userId", 255};
	{"P5userId", 255};
	{"P6userId", 325};
	{"P7userId", 310};
	{"P8userId", 225};
	{"P9userId", 200};
	{"P10userId", 175};
	{"P11userId", 150};
	{"P12userId", 225};
    {"P13userId", 245};
}
for _, Table in pairs(QueueTable) do
	table.sort(Table, function(a,b)
		return a[1] < a[2]
	end)
end
--print(unpack(QueueTable))

for _, Table in pairs(QueueTable) do
	for i, int in pairs(Table) do
		if i == 2 then
			print(int)
		end
	end
end

In the last for loop, I’m able to separate the MMR values from the string values. However, when I try sorting it in the previous for loop. I get an error for attempting to index a number with a number at “return a[1] < a[2]” a is the MMR value and b is the string value. I want to sort the tables by lowest MMR value to highest. I can’t separate the MMR value from the string value since that will be holding the userid so the server knows who to match who.
Once the tables are sorted It would simplify match making process. How can I sort the tables?

An approach that comes to mind is trying to get the sum of the teams MMR values as close as you can. This can be done with some simple code (depending on team size and value spread) by simply adding a player to the team with the lowest total MMR each time.

1 Like

That would be a good simple method. The only issue I see is there’s a possibility of the higher MMR person getting ignored. I guess you could always switch it up. I do eventually want to use memory store to queue players from different servers so it will get more complicated with more players joining.

I thought about using sorting to make it easier, but I had challenges sorting QueueTable since it’s tables within a table with both int and string values.

I’m going to edit this post since I wasn’t specific enough.