Jump to content

Module:Sandbox/Jackmcbarn

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jackmcbarn (talk | contribs) at 02:01, 18 July 2014 (handle first names too). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local function split(str, delim)
	local retval, endpos = {}, 1
	for elem, pos in str:gmatch('(.-)' .. delim .. '()') do
		table.insert(retval, elem)
		endpos = pos
	end
	table.insert(retval, str:sub(endpos))
	return retval
end

function p.main(frame)
	local list = mw.text.trim(frame.args[1])
	local listEntries = split(list, '\n')
	table.sort(listEntries, function(a,b)
		local aWords, bWords = split(a, ' ')[2], split(b, ' ')[2]
		return (aWords[2] .. aWords[1]) < (bWords[2] .. bWords[1])
	end)
	return table.concat(listEntries, '\n')
end

return p