简体   繁体   English

按时间对lua中的数组/表进行排序

[英]Sorting Array/ table in lua by time

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. 我试图从方法中获取playerValues并将它们存储到表中(我相信我已经走了这么远),然后我想按时间对这些值进行排序。

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. 我知道表是完全错误的,我仍在学习LUA并研究表功能。

Update, Should I store data like this? 更新,我应该这样存储数据吗?

Example 1:? 示例1:

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

or Example 2: 或示例2:

local sellHistory = {
  eventType = {},
  secsSinceEvent = {}
}

What my array looks like 我的数组看起来像什么

You mean what my array s look like, because you posted 8 of them. 您的意思是我的数组样子,因为您张贴了其中的8个。 You only need one: 您只需要一个:

sellHistory = {}

Each element of this array should contain an event info record, which has fields for eventType , secsSinceEvent , etc. 该数组的每个元素都应包含一个事件信息记录,其中包含eventTypesecsSinceEvent等字段。

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. 顺便说一句,我不知道您要针对哪个API进行编码,而是仅通过名称secsSinceEvent判断-您的逻辑可能已损坏。

If an event occurs at 1:00:00 and you call GetPlayerInfo at 1:00:01, the secsSinceEvent will be 1. 如果事件在1:00:00发生,并且您在1:00:01调用GetPlayerInfo ,则secsSinceEvent将为1。

If an event occurs at 2:00:00 and you call GetPlayerInfo at 2:00:50, the secsSinceEvent will be 50. 如果某个事件在2:00:00发生,并且您在2:00:50调用GetPlayerInfo ,则secsSinceEvent将为50。

If you sort by secsSinceEvent you'll show the second event as older than the first event, which is not true. 如果按secsSinceEvent排序, secsSinceEvent第二个事件显示为比第一个事件更早,这是不正确的。

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. 如果您的目的只是维护secsSinceEvent事件时间排序的历史记录,则需要使用secsSinceEvent来计算事件的时间,方法是从当前时间中减去secsSinceEvent并将其保存为事件的时间戳。 I'm not sure why the API doesn't simply give you the event time in the first place. 我不确定为什么API不会仅仅为您提供事件时间。 Perhaps they want to side step time zone issues. 也许他们想回避时区问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM