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 SCRIPT
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
1 Like
Use index
of the frame or whatever you’re using to list the updates. Then have it add 1 each time a new update log is added.
That will sort it from oldest to newest. I want it to sort newest to oldest
Also I tried that, and that still didn’t fix the issue of the module going through the tables in a random order.
Try sorting the table using table.sort()
.
Maybe try this? Kind of tired at the moment.
table.sort(UpdatelogModule, function(a,b) return tonumber(a["Date"]) > tonumber(b["Date"]) end)
I don’t think this will work because of how your module is setup.
yeah, I tried it and it didn’t work
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.
1 Like
It already does that
UpdateInfoFrame.Name = i .. " " .. UpdateInfo.Date
Is the sort order set to Name
?
It wasn’t, but I just set it to name and i’m still getting the same results.
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)
1 Like
This seemed to have damn worked. Thank you very much!