I’m trying to sort a table which has its value as another table.
How the table is built, is
local cmdData = {
["Command"] = context.Name,
["UserID"] = context.Executor.UserId,
["UserName"] = context.Executor.Name,
["RawText"] = context.RawText,
["Response"] = context.Response,
["Time"] = os.date("%c"),
["Second"] = os.time()
}
You see, I’m saving this to a database, the first key is the players userid, the second key is the Second. I’m then trying to sort the tables by the second, since I want to see the newest command last.
This is how I’m storing it in a datastore
local key = data.Second
local userIdKey = tostring(executorid)
local playerData = commandStorage:GetAsync(userIdKey) or {}
playerData[tostring(key)] = data
local success, errorMessage = pcall(function()
commandStorage:SetAsync(userIdKey, playerData)
end)
And here is where I’m calling it, and sorting it.
local vid = players:GetUserIdFromNameAsync(tostring(victim))
if not vid then return "No user found by that name, try again." end
local log = data:GetAsync(vid)
if not log then return "Player has used no commands, or no commands have been found." end
table.sort(log, function(a,b)
return tonumber(a) > tonumber(b)
end)
for i,v in log do
if v.Command ~= "ModLogs" then
context:Reply("Executor : "..v.UserName..", Command : "..v.Command..", Response : "..v.Response..", Time : "..v.Time)
context:Reply("Raw Command : "..v.RawText)
context:Reply("---------------")
end
end
The problem is, this really should work, yet it doesn’t. for some reason the table.sort isn’t even going through I don’t think, I’m not too sure how table.sort works, but if I run it or I don’t, the output is always the same.