2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/plugins/statistics.koplugin/tableutil.lua
Alexander Pletnev c45328f14e Add Statistic plugin (#1581 Amount of hours spent on a book)
Changes based on comments
2015-09-13 22:34:20 +03:00

35 lines
782 B
Lua

local tableutil = {}
--http://stackoverflow.com/questions/15706270/sort-a-table-in-lua
function tableutil.spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys + 1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a, b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
function tableutil.tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
return tableutil