Jump to content

Module:Infobox sort

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Brooklynpedestrian (talk | contribs) at 12:02, 9 June 2020 (Make asc function similarly to desc). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

function p.asc(frame)
    list = {}
	for key,value in pairs(frame.args) do
		-- Remove newlines
		stripped = string.gsub(value, "\n", "")
		
		if stripped:match("%S") ~= nil then
		    table.insert(list, stripped)
		end
	end
    table.sort( list, function (a, b)
    	indexA = a:find("%%")
    	indexB = b:find("%%")
    	numberA = tonumber(a:sub(1, indexA - 1))
    	numberB = tonumber(b:sub(1, indexB - 1))
    	return numberA < numberB 
    end )
    return table.concat( list, "<br>" )
end

function p.desc(frame)
	list = {}
	for key,value in pairs(frame.args) do
		-- Remove newlines
		stripped = string.gsub(value, "\n", "")
		
		if stripped:match("%S") ~= nil then
		    table.insert(list, stripped)
		end
	end
    table.sort( list, function (a, b)
    	indexA = a:find("%%")
    	indexB = b:find("%%")
    	numberA = tonumber(a:sub(1, indexA - 1))
    	numberB = tonumber(b:sub(1, indexB - 1))
    	return numberA > numberB 
    end )
    return table.concat( list, "<br>" )
end

return p