Jump to content

Module:Infobox sort: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Remove debug logging
Make asc function similarly to desc
Line 6: Line 6:
-- Remove newlines
-- Remove newlines
stripped = string.gsub(value, "\n", "")
stripped = string.gsub(value, "\n", "")
stripped = string.gsub(value, "\r", "")
if stripped ~= nil and stripped ~= "" and stripped ~= "\n" and stripped ~= " " and stripped ~= "\r" then
if stripped:match("%S") ~= nil then
table.insert(list, stripped)
table.insert(list, stripped)
end
end
end
end
table.sort( list )
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>" )
return table.concat( list, "<br>" )
end
end

Revision as of 12:02, 9 June 2020

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