Question

I've been researching for quite a while on sorting tables, I see plenty of examples but I can't seem to get them working.

I'm trying to get playerValues from a method and store them into a table (I believe I'm this far) then I want to sort those values by time.

Example code:

What my array looks like

sellTest = {}
sellTest.eventType = {}
sellTest.secsSinceEvent = {}
sellTest.buyer = {}
sellTest.seller = {}
sellTest.someParam = {}
sellTest.itemName = {}
sellTest.soldAmount = {}

How I'm storing the values

for j = 0, totalEvents do
  for i = 0, numEvents do
    sellTest.eventType[i], sellTest.secsSinceEvent[i], sellTest.seller[i],
    sellTest.buyer[i], sellTest.someParam[i], sellTest.itemName[i],
    sellTest.soldAmount[i] = GetPlayerInfo(event(j),SELL_SALES, i )
  end
 end

Then How I'm trying to display the table (Stuck here)

local function sortMyTable()
  table.sort(sellTest, function(a, b) return secsSinceEvent[2]  > secsSinceEvent[2] end)

  for k,v in ipairs(sellTest) do
    print(v[1].. ' == '.. v[2])
  end
end

I know that table is completely wrong, I'm still learning LUA and researching the table functionality.

Update, Should I store data like this?

Example 1:?

local sellTest = {}
    sellTest['eventType'] = {}
    sellTest['secsSinceEvent'] = {}

or Example 2:

local sellHistory = {
  eventType = {},
  secsSinceEvent = {}
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top