Module:Sandbox/Swpb/surname-sort

local p = {}

function p.surname(frame) local fullname = frame.args[1] --Pull off any generational suffix suffix1_index = mw.ustring.find(fullname, ',? [JS]n?r%.?$') suffix2_index = mw.ustring.find(fullname, ' [IVX]+$') if type(suffix1_index) ~= 'nil' then suffix = mw.ustring.sub(fullname, suffix1_index) nosuffix = mw.ustring.sub(fullname, 1, suffix1_index-1) elseif type(suffix2_index) ~= 'nil' then suffix = mw.ustring.sub(fullname, suffix2_index) nosuffix = mw.ustring.sub(fullname, 1, suffix2_index-1) else suffix = '' nosuffix = fullname end

--[[	--Pull off last word --	lastword_index = mw.ustring.find(nosuffix, '[ ^]%S+$')	if type(mw.ustring.find(nosuffix, ' %S+$')) == 'nil' then		lastword_index = mw.ustring.find(nosuffix, '^%S+$')	else		lastword_index = mw.ustring.find(nosuffix, ' %S+$')				end

lastword = mw.ustring.sub(nosuffix, lastword_index) nolastword = mw.ustring.sub(nosuffix, 1, lastword_index-1)

--Pull off second to last word --	secondtolastword_index = mw.ustring.find(nolastword, '[ ^]%S+$') secondtolastword_index = mw.ustring.find(nolastword, ' %S+$') or mw.ustring.find(nolastword, '^%S+$') secondtolastword = mw.ustring.sub(nolastword, secondtolastword_index) nosecondtolastword = mw.ustring.sub(nolastword, 1, secondtolastword_index-1) secondtolastword_lower = mw.ustring.lower(secondtolastword)

prefixes = {'a', 'ab', 'af', 'av', 'ap', 'abu', 'al', 'ālam', 'alam', 'bar', 'bath', 'bat', 'bean', 'ben', 'bet', 'bin', 'bint', 'da', 'das', 'de', 'degli', 'del', 'dele', 'della'} .. { 'der', 'di', 'dos', 'du', 'e', 'el', 'fetch', 'vetch', 'fitz', 'i', 'ibn', 'kil', 'gil', 'la', 'le', 'lu', 'm\'', 'mac', 'mc', 'mck', 'mhic', 'mic', 'mag', 'mala', 'maol', 'mug'} .. {'na', 'neder', 'ní', 'ni', 'nic', 'nig', 'nin', 'nord', 'norr', 'ny', 'ó', 'o', 'öfver', 'ofver', 'ost', 'öst', 'öster', 'øst', 'över', 'over', 'öz', 'pour', 'saint', 'san'} .. {'ste', 'stor', 'söder', 'soder', 'sønder', 'sonder', 'ter', 'tre', 'ua', 'uí', 'ui\'', 'ui', 'van', 'väst', 'vast', 'väster', 'vaster', 'vest', 'von', 'woj', 'zu'} local surname, givenname for prefix in prefixes do		if secondtolastword_lower == prefix then surname = secondtolastword .. ' ' .. lastword givenname = nosecondtolastword end end if secondtolastword_lower == 'der' or secondtolastword_lower == 'y' then --		thirdtolastword_index = mw.ustring.find(nosecondtolastword, '[ ^]%S+$') thirdtolastword_index = mw.ustring.find(nosecondtolastword, ' %S+$') or mw.ustring.find(nosecondtolastword, '^%S+$') thirdtolastwod = mw.ustring.sub(nosecondtolastword, thirdtolastword_index) nothirdtolastword = mw.ustring.sub(nosecondtolastword, 1, thirdtolastword_index-1) thirdtolastword_lower = mw.ustring.lower(thirdtolastword) if (secondtolastword_lower == 'y') or (thirdtolastword_lower == 'van' and secondtolastword_lower == 'der') then surname = thirdtolastword .. ' ' .. secondtolastword .. ' ' .. lastword givenname = nothirdtolastword end else surname = lastword givenname = nolastword end sortname = surname .. ', ' .. givenname .. ' ' .. suffix return sortname --]]	return nosuffix end

return p