statt ..
Code: Alles auswählen
local Filters = {}
function Filters.sortEntries(list)
local mode = conf.sortMode
local function compare(a, b)
if mode == 'date_asc' then
return (a.timestamp or 0) < (b.timestamp or 0)
elseif mode == 'title_asc' then
return (a.title or ''):lower() < (b.title or ''):lower()
elseif mode == 'title_desc' then
return (a.title or ''):lower() > (b.title or ''):lower()
elseif mode == 'duration_desc' then
return (a.durationSec or 0) > (b.durationSec or 0)
elseif mode == 'duration_asc' then
return (a.durationSec or 0) < (b.durationSec or 0)
else -- date_desc default
return (a.timestamp or 0) > (b.timestamp or 0)
end
end
-- TODO: Handling duplicate timestamps/titles could be stabilized if needed
table.sort(list, compare)
end
Code: Alles auswählen
local Filters = {}
function Filters.sortEntries(list)
local mode = conf.sortMode
-- Ursprungsindex merken
for i, entry in ipairs(list) do
entry._index = i
end
local function cmp(aVal, bVal, a, b, asc)
if aVal == bVal then
return a._index < b._index
end
return asc and (aVal < bVal) or (aVal > bVal)
end
-- Mapping-Tabelle: jeder Modus liefert Wert und Richtung
local modes = {
date_asc = function(a, b) return cmp(a.timestamp or 0, (b.timestamp or 0), a, b, true) end,
date_desc = function(a, b) return cmp(a.timestamp or 0, (b.timestamp or 0), a, b, false) end,
title_asc = function(a, b) return cmp((a.title or ''):lower(), (b.title or ''):lower(), a, b, true) end,
title_desc = function(a, b) return cmp((a.title or ''):lower(), (b.title or ''):lower(), a, b, false) end,
duration_asc = function(a, b) return cmp(a.durationSec or 0, (b.durationSec or 0), a, b, true) end,
duration_desc= function(a, b) return cmp(a.durationSec or 0, (b.durationSec or 0), a, b, false) end,
}
-- Fallback: wenn Modus unbekannt, nimm date_desc
local compare = modes[mode] or modes.date_desc
table.sort(list, compare)
end
