What my array looks like
You mean what my arrays look like, because you posted 8 of them. You only need one:
sellHistory = {}
Each element of this array should contain an event info record, which has fields for eventType
, secsSinceEvent
, etc.
for i = 0, numEvents do
local e = {}
e.eventType, e.secsSinceEvent, e.seller, e.buyer, e.someParam, e.itemName, e.soldAmount
= GetPlayerInfo(event(j), SELL_SALES, i )
table.insert(sellHistory, e)
end
Now sorting the event info by time is easy:
table.sort(sellHistory, function(a,b) return a.secsSinceEvent > b.secsSinceEvent end)
Then (for example):
for i,event in ipairs(sellHistory) do
print(event.eventType, event.secsSinceEvent, event.seller)
end
What you were doing is taking the event info, writing each value into separate arrays which were correlated by index. This makes stuff like sorting very difficult, as you discovered.
By the way, I don't know what API you're coding against, but judging only by the name -- secsSinceEvent
-- your logic is probably broken.
If an event occurs at 1:00:00 and you call GetPlayerInfo
at 1:00:01, the secsSinceEvent
will be 1.
If an event occurs at 2:00:00 and you call GetPlayerInfo
at 2:00:50, the secsSinceEvent
will be 50.
If you sort by secsSinceEvent
you'll show the second event as older than the first event, which is not true.
If your purpose is just to maintain a history sortable by event time, you need to use secsSinceEvent
to calculate the time of the event by subtracting secsSinceEvent
from the current time and saving that as the timestamp for the event. I'm not sure why the API doesn't simply give you the event time in the first place. Perhaps they want to side step time zone issues.