How could I sort a table with instances by the instance name

I’m currently attempting to sort a table that is created from :GetChildren() alphabetically in the following code

local ImageButtons = {}
for _, button in ipairs(GUI.Frame:GetChildren()) do -- Loops through all children inside GUI.Frame
		if button:IsA("ImageButton") then -- Checks if the child is an ImageButton
			table.insert(ImageButtons, button) -- Adds the ImageButton to the library
		end
	end
	table.sort(ImageButtons)

The table.sort gives the error “attempt to compare Instance < Instance” which I understand is because it is trying to compare the instances inside the table. Is there any way that I can sort the table?

4 Likes
table.sort(ImageButtons, function(a,b))
	return a.Name < b.Name
end

Simply use the instance’s name for the comparison function.

3 Likes

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