Modul:SK/Sort
< Modul:SK
Script error: The function "nonexistent" does not exist.
--------------------------------------------------------------------
-- Modul:SK/Sort
--------------------------------------------------------------------
local s = {}
local db = require("Modul:SK").db
local function collect()
local t = {}
for code, d in pairs(db) do
t[#t+1] = {
code = code,
naziv = d.naziv,
pop = d.pop or 0,
area = d.area or 0,
density = d.density or 0,
}
end
return t
end
local function cmp(field, desc)
return function(a, b)
if a[field] == b[field] then
return a.naziv < b.naziv
end
if desc then
return a[field] > b[field]
else
return a[field] < b[field]
end
end
end
function s.top(field, n, desc)
n = tonumber(n) or 10
local t = collect()
table.sort(t, cmp(field, desc))
local out = {}
for i = 1, math.min(n, #t) do
local r = t[i]
out[#out+1] = r
end
return out
end
return s