How to get an item number/position inside a UIListLayout sorted by name?

I have this list of fonts inside a ScrollingFrame, sorted alphabetically by name:



If I print the ScrollingFrame content, I get an unsorted list

for i, v in pairs(SFrameFontes:GetChildren()) do
	print("i, v: ", i, v)
end
		  "i, v: "   1   UIListLayout
		  "i, v: "   2   Ubuntu
		  "i, v: "   3   Legacy
		  "i, v: "   4   Arial
		  "i, v: "   5   ArialBold
		  "i, v: "   6   SourceSans
		  "i, v: "   7   SourceSansBold
		  "i, v: "   8   SourceSansSemibold
		  "i, v: "   9   SourceSansLight
		  "i, v: "   10   SourceSansItalic
		  "i, v: "   11   Bodoni

But I need to get the numbers of the items in its sorted positions, for example: “Arial” must be 1, “ArialBold” = 2, “Boldoni” = 3, and so on…

How to get an item number/position inside a UIListLayout sorted by name?

You have to use ipairs as it returns it in numerical order. I thought pairs or ipairs works either way.

You can use table.sort to sort a table. Just sort with a custom function that compares names. Here’s a quick example that makes sure to account for capitalization:

local children = whatever:GetChildren() -- doesnt matter

table.sort(children, function(first, second)
	-- returns whether first should be before second in the list
	return first.Name:lower() < second.Name:lower()
end)

for i, v in ipairs(children) do -- remember to use ipairs for guaranteed order
	print(i, v) --> should be in order now
end
4 Likes