This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

lua table find higest value
#3
(23.06.2020, 06:28)admin Wrote: You can find the maximum value like this. If two items have the same on_time then the one that appears first in the table will be returned.
Code:
max_index = 0
max_time = 0

for index, item in ipairs(t) do
  if item.on_time > max_time then
    max_index = index
    max_time = item.on_time
  end
end

max_item = t[ max_index ]
if max_item then
  log(max_item)
end

Or you can sort the table using custom comparison function. In this case the order of elements in not defined if on_time is the same. t[1] will contain the element with maximum on_time value.
Code:
table.sort(t, function(a, b)
  return a.on_time > b.on_time
end)

log(t)
Many Thanks, i have it working
Reply


Messages In This Thread
RE: lua table find higest value - by admin - 23.06.2020, 06:28
RE: lua table find higest value - by benanderson_475 - 23.06.2020, 22:13

Forum Jump: