<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASort_title%2Fsandbox</id>
	<title>Module:Sort title/sandbox - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASort_title%2Fsandbox"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sort_title/sandbox&amp;action=history"/>
	<updated>2026-04-22T04:38:40Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sort_title/sandbox&amp;diff=146850&amp;oldid=prev</id>
		<title>imported&gt;Gonnym at 01:13, 26 December 2019</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sort_title/sandbox&amp;diff=146850&amp;oldid=prev"/>
		<updated>2019-12-26T01:13:57Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local getArgs = require(&amp;#039;Module:Arguments&amp;#039;).getArgs&lt;br /&gt;
local match = require(&amp;quot;Module:String&amp;quot;)._match&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return a title without its first word.&lt;br /&gt;
--]]&lt;br /&gt;
local function getTitleWithoutFirstWord(title)&lt;br /&gt;
	return mw.ustring.gsub(title, &amp;quot;^[^%s]*%s*&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to check if the word is an article.&lt;br /&gt;
Returns true if it is, false if it isn&amp;#039;t.&lt;br /&gt;
--]]&lt;br /&gt;
local function isFirstWordAnArticle(word)&lt;br /&gt;
	word = string.lower(word)&lt;br /&gt;
	if (word == &amp;quot;a&amp;quot; or word == &amp;quot;an&amp;quot; or word == &amp;quot;the&amp;quot;) then&lt;br /&gt;
		return true&lt;br /&gt;
	else&lt;br /&gt;
		return false&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return a sort key for a specific part.&lt;br /&gt;
--]]&lt;br /&gt;
local function getSortKey(title, firstWord)&lt;br /&gt;
	local sortKey = title&lt;br /&gt;
	if (isFirstWordAnArticle(firstWord) and firstWord ~= title) then&lt;br /&gt;
		title = getTitleWithoutFirstWord(title)&lt;br /&gt;
		sortKey = title .. &amp;quot;, &amp;quot; .. firstWord&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	return sortKey&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--TODO: DOC&lt;br /&gt;
local function convertRomanNumerals(romanNumerals)&lt;br /&gt;
	local convertModule = require(&amp;#039;Module:ConvertNumeric&amp;#039;).roman_to_numeral&lt;br /&gt;
	local result, error = convertModule(romanNumerals)&lt;br /&gt;
	if (tonumber(result) ~= -1) then&lt;br /&gt;
		return tonumber(result)&lt;br /&gt;
	else&lt;br /&gt;
		return nil&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--TODO: DOC&lt;br /&gt;
local function checkForRomanNumerals(title)&lt;br /&gt;
	if (title) then&lt;br /&gt;
		return mw.ustring.match(title, &amp;quot;[IVXLCDM]+&amp;quot;)&lt;br /&gt;
	else&lt;br /&gt;
		return nil&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return the last word from a title.&lt;br /&gt;
--]]&lt;br /&gt;
local function getLastWord(title)&lt;br /&gt;
	return string.match(title, &amp;quot;%s(%S+)$&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return the first word from a title.&lt;br /&gt;
--]]&lt;br /&gt;
local function getFirstWord(title)&lt;br /&gt;
	return match(title, &amp;quot;^[^%s]*&amp;quot;, 1, 1, false, &amp;quot;&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return a number without commas.&lt;br /&gt;
For example: 4,722 will become 4722.&lt;br /&gt;
--]]&lt;br /&gt;
local function cleanNumberFromCommas(title)&lt;br /&gt;
	return string.gsub(title, &amp;quot;(%d+)(,+)&amp;quot;, &amp;quot;%1&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--TODO: DOC&lt;br /&gt;
local function getTitlePartSortKey(title)&lt;br /&gt;
	title = cleanNumberFromCommas(title)&lt;br /&gt;
	local firstWord = getFirstWord(title)&lt;br /&gt;
	local lastWord = getLastWord(title)&lt;br /&gt;
	local romanNumerals = checkForRomanNumerals(lastWord)&lt;br /&gt;
	if (romanNumerals) then&lt;br /&gt;
		local lastWordModified = convertRomanNumerals(lastWord)&lt;br /&gt;
		if (lastWordModified) then&lt;br /&gt;
			title = string.gsub(title, lastWord, lastWordModified)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return getSortKey(title, firstWord)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return the disambiguation sort key.&lt;br /&gt;
--]]&lt;br /&gt;
local function getDisambiguationSortKey(disambiguation)&lt;br /&gt;
	if (disambiguation == &amp;quot;&amp;quot;) then&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return &amp;quot;(&amp;quot; .. getTitlePartSortKey(disambiguation) .. &amp;quot;)&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return the title without its disambiguation.&lt;br /&gt;
--]]&lt;br /&gt;
local function getTitleWithoutDisambiguation(title, disambiguation)&lt;br /&gt;
	local newTitle = string.gsub(title, &amp;quot;%(&amp;quot;.. disambiguation .. &amp;quot;%)&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	return mw.text.trim(newTitle)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Local function which is used to return the disambiguation from a title.&lt;br /&gt;
--]]&lt;br /&gt;
local function getDisambiguation(title)&lt;br /&gt;
	local disambiguation = match(title, &amp;quot;%s%((.-)%)&amp;quot;, 1, -1, false, &amp;quot;&amp;quot;)&lt;br /&gt;
	if (disambiguation == &amp;quot;&amp;quot;) then&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	else&lt;br /&gt;
		return disambiguation&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
The main function.&lt;br /&gt;
--]]&lt;br /&gt;
local function _main(args)&lt;br /&gt;
	local title&lt;br /&gt;
	if (args[1]) then&lt;br /&gt;
		title = args[1]&lt;br /&gt;
	else&lt;br /&gt;
		title = mw.title.getCurrentTitle().text&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local disambiguation = getDisambiguation(title)&lt;br /&gt;
	local disambiguationSortKey = getDisambiguationSortKey(disambiguation)&lt;br /&gt;
	&lt;br /&gt;
	title = getTitleWithoutDisambiguation(title, disambiguation)&lt;br /&gt;
	title = getTitlePartSortKey(title)&lt;br /&gt;
&lt;br /&gt;
	local sortKey = title .. &amp;quot; &amp;quot; .. disambiguationSortKey&lt;br /&gt;
	return mw.text.trim(sortKey)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Public function which allows modules to retrieve a sort key.&lt;br /&gt;
--]]&lt;br /&gt;
function p._getSortKey()&lt;br /&gt;
	return _main(nil)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Public function which allows templates to retrieve a sort key.&lt;br /&gt;
--]]&lt;br /&gt;
function p.getSortKey(frame)&lt;br /&gt;
	local args = getArgs(frame)&lt;br /&gt;
	return p._main(args) &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Public function which allows templates to retrieve the sort key inside a DEFAULTSORT.&lt;br /&gt;
--]]&lt;br /&gt;
function p.getDefaultSort(frame)&lt;br /&gt;
	local args = getArgs(frame)&lt;br /&gt;
	local sortKey = p._main(args)&lt;br /&gt;
	return frame:preprocess{text = &amp;quot;{{DEFAULTSORT:&amp;quot; .. sortKey .. &amp;quot;}}&amp;quot;} &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Public function which is used for testing various names and not the current page name.&lt;br /&gt;
--]]&lt;br /&gt;
function p.testcases(frame)&lt;br /&gt;
	local args = getArgs(frame)&lt;br /&gt;
	return _main(args)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Gonnym</name></author>
	</entry>
</feed>