Jump to content

Module:Infobox/utilities

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 17:47, 19 July 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;


--[[--------------------------< C O M P >----------------------------------------------------------------------

compare function for result{} table descending sort

]]

local function comp (a, b)
	return tonumber (a[1]) > tonumber (b[1]);
end
	

--[[--------------------------< E T H N I C I T Y _ S O R T >--------------------------------------------------

{{#invoke:Sandbox/trappist the monk/ethnicity sort|ethnicity_sort|{{{percent white|}}}|{{{percent black|}}}|{{{percent asian|}}}|{{{percent hispanic|}}}|{{{percent native american|}}}|{{{percent native hawaiian|}}}|{{{percent more than one race|}}}|{{{percent other race|}}} }}

]]

local function ethnicity_sort (frame)
	local args=getArgs(frame);													
	local result = {															-- initialize; table will be sorted according to values in result[n][1]
		{args[1], '% [[White people|white]]'},
		{args[2], '% [[Black people|black]]'},
		{args[3], '% [[Asian Americans|Asian]]'},
		{args[4], '% [[Hispanic]]'},
		{args[5], '% [[Native Americans in the United States|Native American]]'},
		{args[6], '% [[Pacific Islands Americans]]'},
		{args[7], '% [[Two or more races]]'},
		{args[8], '% other'},
	};
	
	for i=8, 1, -1 do
		if not tonumber (result[i][1]) then										-- if cannot be converted to a number
			table.remove (result, i);											-- delete
		end
	end
	table.sort (result, comp);													-- sort what remains

	for i, v in ipairs (result) do
		result[i] = table.concat (result[i]);									-- make each table in result{} a string
	end
	
	result[1] = table.concat ({result[1], args[9] and args[9] or ''});
	
	return frame:expandTemplate { title = 'Unbulleted list', args = result};	-- render the unbulleted list
end


--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------

]]

return {
	ethnicity_sort = ethnicity_sort,
	}