Module:AirdateOrder/sandbox

local p = {} function p.airdateOrder(frame) local ep = frame.args[1] local order = frame.args["order"] local epMatcher = require('Module:Ep/Matcher') local epCode = epMatcher.matchCode(ep) local airDate = frame.args['airdate'] local value local airArray = mw.loadData('Module:AirdateOrder/Array') table.sort(airArray, compare)

if order == "next" then value = p.nextEp(airArray, epCode) elseif order == "prev" then value = p.prevEp(airArray, epCode) elseif airDate == '1' then value = p.airdate(airArray, epCode) end return frame:preprocess(value) end

function getEp(airArray, index) local epCode = '0x00' if airArray[index] then epCode = airArray[index]['epCode'] end if epCode == '0x00' then epCode = "N/A" else epCode = "" end return epCode end

function getIndex(airArray, epCode) local index = 0 local i = 1; while airArray[i] do       if airArray[i]['epCode'] == epCode then index = i                break end i = i + 1 end return index end

function convert_to_date(s) local p="(%d+)-(%d+)-(%d+)" year,month,day=s:match(p) convertedDate = os.time({day=day,month=month,year=year}) return convertedDate end

function compare(a,b) a_date = convert_to_date(a.date) b_date = convert_to_date(b.date) return a_date < b_date end function p.nextEp(airArray, epCode) local index = getIndex(airArray, epCode) index = index + 1 local epCode = getEp(airArray, index) return epCode end function p.prevEp(airArray, epCode) local index = getIndex(airArray, epCode) index = index - 1 local epCode = getEp(airArray, index) return epCode end

function p.airdate(airArray, epCode) local airDate local index = getIndex(airArray, epCode) if airArray[index] then airDate = airArray[index]['date'] end return airDate end

return p