So I am trying to make an update log GUI that shows all the updates from a module. My issue is that I want it descend from newest to oldest. But as shown below it appears to sort randomly
local function SetupUpdatelog()
for i, UpdateInfo in pairs(UpdatelogModule) do
local UpdateInfoFrame = UpdateInfoTemplate:Clone()
local UpdateTextTemplate = UpdateInfoFrame:WaitForChild("UpdateInfoText")
UpdateInfoFrame.Name = i .. " " .. UpdateInfo.Date
UpdateInfoFrame.UpdateTitle.Text = UpdateInfo.GameVersion .. " - " .. UpdateInfo.Title
UpdateInfoFrame.UpdateTitle.Date.Text = UpdateInfo.Date
for i2, UpdateContent in pairs(UpdateInfo.UpdateList) do
local UpdateText = UpdateTextTemplate:Clone()
UpdateText.Text = "- " .. UpdateContent
UpdateText.Parent = UpdateInfoFrame
end
UpdateTextTemplate:Destroy()
UpdateInfoFrame.Parent = UpdatesContainer
end
end
MODULE
local Updatelog = {
Update1 = {
Title = "A Fresh Start",
GameVersion = "0.0.0 IN-DEV",
Date = "23/9/2020",
UpdateList = {
"Development began",
},
},
Update2 = {
Title = "Some Small Update",
GameVersion = "0.1.0 IN-DEV",
Date = "12/11/2020",
UpdateList = {
"GUI posted to the Roblox Developer Forum",
"This is some very long text that is soo long that you have to scroll to see the rest of the update",
"Generic update blah blah blah",
"Bug fixes",
},
},
Update3 = {
Title = "The Future",
GameVersion = "51.2.1 FULL RELEASE",
Date = "12/11/2023",
UpdateList = {
"Jumped a few years into the future",
"Gui has finally been finished",
"Game has reached 7B+ visits",
},
},
}
return Updatelog
If you want to do it a simple way you can use the UIListlayout, and then name the update the “Update1” or “Update2” and if you set how it sorts in the layout, it should sort automatically.
Updatelog is a dictionary; pairs will iterate through a dictionary in an undefined order, because they are not meant to have any order.
Arrays have an order. Make Updatelog an array, and sort it.
Essentially, just remove the Update1 = , Update2 = etc. from it and your script will put them in a consistent order.
To sort the updates by date, use table.sort with a custom comparison function that compares by date, like so: (goes between Updatelog and return Updatelog after making Updatelog an array)
local function dateToTime(date)
-- Date is assumed to be in D/M/YYYY format
-- Convert dates to a number, whose higher values are later and smaller values are earlier
-- Will likely suffer from Year 2038 Bug
local day, month, year = string.match(date, "(%d%d?)/(%d%d?)/(%d%d%d%d)")
day, month, year = tonumber(day), tonumber(month), tonumber(year)
assert(day and month and year, "Malformed date: " .. date)
return os.time({day = day, month = month, year = year})
end
-- Sort descending from newest (highest time) to oldest (lowest time)
table.sort(Updatelog, function(a, b)
return dateToTime(a.Date) > dateToTime(b.Date)
end)