How To Sort Items In "for" Function?

Hey Developers! I’m having trouble with sorting UI items in my research tree UI. Here’s an example of what I mean by that:

And the problem is that when I create these buttons through "for i, ..." function, I have no idea how to place them correctly in order; like the tree starts of Pistol, then after you buy Pistol, you unlock a more advanced gun.

Do You know how to implement this in code? Or some examples or suggestions? I’ll be glad for any help!

Wouldn’t ipairs place it in numerical order and thus ordering it?

1 Like

Thank You for the idea, but can you share with some examples of code if you can? Because it’s gonna be the first time i work with ipairs, I’ll really appreciate it!

Oh nevermind. I’ve already found a post, which describes it pretty accessible here

Do you have a table of the items you need in the correct order, for example Pistol at index 1, and so on? If not, you’ll need to sort it.

I’d recommend having a table of the desired item order with number references., where 1 is the top and the largest number is the bottom.

However, if you are using nested tables, for example for each category of weapon, you’ll need a more complex approach.

Here’s a quick example using table.sort to sort the items based off of priority.

local priorities = { --priorities; the order
	["Guns"] = {
		["Pistol"] = 1,
		["Gun2"] = 2,
		["Gun3"] = 3
	},
	
	["Planes"] = {
		["Plane1"] = 1,
		["Plane2"] = 2,
		["Plane3"] = 3
	}
}

local items = { --the items themselves
	["Guns"] = {
		"Gun3",
		"Gun2",
		"Pistol"
	},
	
	["Planes"] = {
		"Plane3",
		"Plane1",
		"Plane2"
	}
}

for k, v in next, items, nil do
	table.sort(v, function(a, b)
		return priorities[k][a] < priorities[k][b]
	end)
end

table.sort in Luau uses a variation of the Quicksort algorithm, which is pretty efficient at sorting items. However, for more nested data structures it may eventually become more efficient to look into other sorting algorithms like a Merge sort for example, which is inherently recursive and may be more efficient if you have more nested data structures, and could be good after a bit of modification.

Any questions, please ask.

1 Like

Will I have to use the merge sort thing if I have multiple research trees in different UI Frames? Like if I have “Guns” research tree and “Planes” research tree?

It depends if you are using the same data structure for the data itself, whether it’s in different UI frames won’t be relevant. Basically, if you keep all the nested tables under one main table, or all the data within one main table. Otherwise, they will need to be separately sorted.

1 Like

So I have the same data and ± structure, so I don’t really need it?

You shouldn’t need the merge sort unless your data structure goes really deep in which case it would be better. A standard table.sort like my example should do for now.

1 Like

Alright then! Big thank You! Gonna try my best not to mess up!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.