How to reverse the content of dictionary (or this table)?

Im making a update log UI, but since i definitely don’t want to do the primitive method of manually editting the UI by hand. I made a script to automaticly do the UI writing for me, and all i have to do is update the log.

While it is working as expected, i would love to have the table reversed so while the most recent update is at the bottom of the script, the actual recent update log appear at the top. Sure, i can just reverse the table writing, but i just dislike writing my newest update at the top of the script.

Is there any way i can reverse this table (or dictionary, whatever this type is called) content?

local ScrollingFrame = script.Parent.Frame.ScrollingFrame
local textLabel = ScrollingFrame.TextLabel

local headerSize = 40

local function Header(text)
	return [[<font size="]]..tostring(headerSize)..[[">]]..text..[[</font>]]
end

-- Please add the recent update at the top. Because its look like sorting dictionary normally is impossible
local update = {
	{
		Header("Version 1"),
		"Stuff"
	},
	{
		Header("Version 2"),
		"Stuff"
	},
	{
		Header("Version 3"),
		"Stuff"
	},
	{
		Header("Version 4"),
		"-Stuff"
	},
	
	
}

for i = 1,#update do
	local j = #update - i + 1
	update[i], update[j] = update[j], update[i]
end

local actualText = ""

for i,subTxt in pairs(update) do
	for i, txt in ipairs(subTxt) do
		actualText = actualText..txt.."\n" -- \n is equal enter
	end
	
	actualText = actualText.."\n"
end

textLabel.Text = actualText

-- Resize the scrolling frame to fit the text label
ScrollingFrame.CanvasSize = UDim2.fromOffset(0,textLabel.TextBounds.Y)

it’s not entirely reversing, but i think you could do something like this?

for i = #update, 1, -1 do
   -- write stuff
end
3 Likes

Try this, it reverses the table.

for i = 1, math.floor(#t/2) do
   local j = #t - i + 1
    t[i], t[j] = t[j], t[i]
end

Example:

You can see from the script i provided at the post that i do indeed already tried that method, since i did some research before asking this question and stumbled on someone post with the same solution, however that code didn’t work for me as the table did not reversed.

Also, huh, never crossed my mind on that. Guess i try, hopefully that work