<?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%3AAutosortTable</id>
	<title>Module:AutosortTable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3AAutosortTable"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:AutosortTable&amp;action=history"/>
	<updated>2026-04-21T04:56:59Z</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:AutosortTable&amp;diff=135332&amp;oldid=prev</id>
		<title>imported&gt;JohnFromPinckney: From sandbox: documentation completed (params missing) &amp; expanded (new params); caption &amp; rowheader now supported; coding per mw:cc</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:AutosortTable&amp;diff=135332&amp;oldid=prev"/>
		<updated>2021-02-21T00:27:13Z</updated>

		<summary type="html">&lt;p&gt;From sandbox: documentation completed (params missing) &amp;amp; expanded (new params); caption &amp;amp; rowheader now supported; coding per &lt;a href=&quot;https://www.mediawiki.org/wiki/cc&quot; class=&quot;extiw&quot; title=&quot;mw:cc&quot;&gt;mw:cc&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--[[&lt;br /&gt;
AutosortTable: Creates a table which is automatically sorted&lt;br /&gt;
 &lt;br /&gt;
Usage: (Remove the hidden comments before use)&lt;br /&gt;
 &lt;br /&gt;
{{#invoke: AutosortTable|create&lt;br /&gt;
 &lt;br /&gt;
| class = wikitable                          &amp;lt;!-- Class for the entire table --&amp;gt;&lt;br /&gt;
| style = width: 50%;                        &amp;lt;!-- CSS for the entire table --&amp;gt;&lt;br /&gt;
| separator = --                             &amp;lt;!-- Separator string used to separate cells; pipe (|) invalid --&amp;gt;&lt;br /&gt;
| order = 2, 1                               &amp;lt;!-- Order for sorting preference, takes a comma-separated list of column numbers --&amp;gt;&lt;br /&gt;
| numeric = 2                                &amp;lt;!-- Columns which use numeric sorting. Takes comma-separated list of column numbers --&amp;gt;&lt;br /&gt;
| descending = 1                             &amp;lt;!-- Columns for which sort order should be descending. Takes comma-separated list of col numbers --&amp;gt;&lt;br /&gt;
| hidden = 2                                 &amp;lt;!-- Columns which are not to be displayed. Takes comma-separated list of col numbers --&amp;gt;&lt;br /&gt;
| rowheader = 1                              &amp;lt;!-- Cell(s) in each non-header row to be emitted as row header, per WP:ACCESS#Data tables. Usually just 1, but accepts comma-separated list of col numbers --&amp;gt;&lt;br /&gt;
| caption = Notable people by age            &amp;lt;!-- Table caption per WP:ACCESS --&amp;gt;&lt;br /&gt;
| header =  -- Name -- Age                   &amp;lt;!-- Table header --&amp;gt;&lt;br /&gt;
| footer =                                   &amp;lt;!-- Table footer, typically a totals row or duplication of header --&amp;gt;&lt;br /&gt;
| -- Bob -- 20                               &amp;lt;!-- Row 1 --&amp;gt;&lt;br /&gt;
| -- Peter -- 35                             &amp;lt;!-- Row 2 --&amp;gt;&lt;br /&gt;
| -- John -- 35                              &amp;lt;!-- Row 3 --&amp;gt;&lt;br /&gt;
| -- James -- 50                             &amp;lt;!-- Row 4 --&amp;gt;&lt;br /&gt;
| background-color: #FFDDDD -- Henry -- 45   &amp;lt;!-- Row 5, with CSS --&amp;gt;&lt;br /&gt;
| colstyle = -- text-align:left; -- text-align:right; -- -- --&lt;br /&gt;
											 &amp;lt;!-- CSS to be used on content of respective columns, here 1st &amp;amp; 2nd --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
}}&lt;br /&gt;
]]&lt;br /&gt;
 &lt;br /&gt;
local _module = {}&lt;br /&gt;
 &lt;br /&gt;
_module.create = function(frame)&lt;br /&gt;
 &lt;br /&gt;
    local args = frame.args&lt;br /&gt;
 &lt;br /&gt;
    -- Named parameters&lt;br /&gt;
 &lt;br /&gt;
    local class = args.class&lt;br /&gt;
    local style = args.style&lt;br /&gt;
    local sep = args.separator&lt;br /&gt;
    local order = args.order&lt;br /&gt;
    local desc = args.descending or &amp;quot;&amp;quot;&lt;br /&gt;
    local nsort = args.numeric or &amp;quot;&amp;quot;&lt;br /&gt;
    local hidden = args.hidden or &amp;quot;&amp;quot;&lt;br /&gt;
    local header = args.header&lt;br /&gt;
    local footer = args.footer&lt;br /&gt;
    local colstyle = args.colstyle&lt;br /&gt;
    local rowheader = args.rowheader or &amp;quot;&amp;quot;&lt;br /&gt;
    local caption = args.caption&lt;br /&gt;
 &lt;br /&gt;
    -- Frequently-used functions&lt;br /&gt;
 &lt;br /&gt;
    local strIndexOf = mw.ustring.find&lt;br /&gt;
    local strSplit = mw.text.split&lt;br /&gt;
    local strSub = mw.ustring.sub&lt;br /&gt;
    local strTrim = mw.text.trim&lt;br /&gt;
 &lt;br /&gt;
    local seplen = #sep&lt;br /&gt;
    local nsortLookup, descLookup, hiddenLookup, rowHeading = {}, {}, {}, {}&lt;br /&gt;
 &lt;br /&gt;
    -- Create the table&lt;br /&gt;
 &lt;br /&gt;
    local html = mw.html.create()&lt;br /&gt;
    local htable = html:tag(&amp;#039;table&amp;#039;)&lt;br /&gt;
    if class then htable:attr(&amp;#039;class&amp;#039;, class) end&lt;br /&gt;
    if style then htable:attr(&amp;#039;style&amp;#039;, style) end&lt;br /&gt;
    if caption then&lt;br /&gt;
    	local hcaption = htable:tag(&amp;#039;caption&amp;#039;)&lt;br /&gt;
    	hcaption:wikitext(caption)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Parses a row string. The &amp;#039;key&amp;#039; parameter is used to assign a unique key to the result so that equal rows do not cause sort errors.&lt;br /&gt;
    local parse = function(s, key)&lt;br /&gt;
        local css&lt;br /&gt;
        local firstSep = strIndexOf(s, sep, 1, true)&lt;br /&gt;
        if firstSep == 1 then  -- no CSS&lt;br /&gt;
            css = nil&lt;br /&gt;
            s = strSub(s, seplen + 1, -1)&lt;br /&gt;
        else   -- CSS before first separator&lt;br /&gt;
            css = strSub(s, 1, firstSep - 1)&lt;br /&gt;
            s = strSub(s, firstSep + seplen, -1)&lt;br /&gt;
        end&lt;br /&gt;
        return {key = key, css = css, data = strSplit(s, sep, true)}&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    --[[&lt;br /&gt;
        Writes a row to the table.&lt;br /&gt;
        css: CSS to apply to the row&lt;br /&gt;
        data: The data (cells) of the row&lt;br /&gt;
        _type: Can be &amp;#039;header&amp;#039;, &amp;#039;footer&amp;#039; or nil.&lt;br /&gt;
    ]]&lt;br /&gt;
    local writeHtml = function(css, data, _type)&lt;br /&gt;
        local row = htable:tag(&amp;#039;tr&amp;#039;)&lt;br /&gt;
        if css then row:attr(&amp;#039;style&amp;#039;, strTrim(css)) end&lt;br /&gt;
 &lt;br /&gt;
        for i, v in ipairs(data) do&lt;br /&gt;
            if not hiddenLookup[i] then&lt;br /&gt;
                local cell&lt;br /&gt;
                if _type == &amp;#039;header&amp;#039; then&lt;br /&gt;
                    -- Header: use the &amp;#039;th&amp;#039; tag with scope=&amp;quot;col&amp;quot;&lt;br /&gt;
                    cell = row:tag(&amp;#039;th&amp;#039;)&lt;br /&gt;
                    cell:attr(&amp;#039;scope&amp;#039;, &amp;#039;col&amp;#039;)&lt;br /&gt;
                elseif _type == &amp;#039;footer&amp;#039; then&lt;br /&gt;
                    -- Footer: Mark as &amp;#039;sortbottom&amp;#039; so that it does not sort when the table is made user-sortable&lt;br /&gt;
                    -- with the &amp;#039;wikitable sortable&amp;#039; class&lt;br /&gt;
                    cell = row:tag(&amp;#039;td&amp;#039;)&lt;br /&gt;
                    cell:attr(&amp;#039;class&amp;#039;, &amp;#039;sortbottom&amp;#039;)&lt;br /&gt;
                else&lt;br /&gt;
                	if rowHeading[i] then&lt;br /&gt;
                		-- Cell is a row heading&lt;br /&gt;
                    	cell = row:tag(&amp;#039;th&amp;#039;)&lt;br /&gt;
                    	cell:attr(&amp;#039;scope&amp;#039;, &amp;#039;row&amp;#039;)&lt;br /&gt;
                    else&lt;br /&gt;
                    	-- Ordinary cell&lt;br /&gt;
                    	cell = row:tag(&amp;#039;td&amp;#039;)&lt;br /&gt;
                    end&lt;br /&gt;
                    local cellCss = colstyle and colstyle[i]&lt;br /&gt;
                    if cellCss then cell:attr(&amp;#039;style&amp;#039;, strTrim(cellCss)) end   -- Apply the column styling, if necessary&lt;br /&gt;
                end&lt;br /&gt;
                cell:wikitext(strTrim(v))&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
        return row&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    -- Parse the column styles&lt;br /&gt;
    if colstyle then colstyle = parse(colstyle, -1).data end&lt;br /&gt;
 &lt;br /&gt;
    -- Write the header first&lt;br /&gt;
    if header then&lt;br /&gt;
        local headerData = parse(header)&lt;br /&gt;
        writeHtml(headerData.css, headerData.data, &amp;#039;header&amp;#039;)&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    -- Parse the data&lt;br /&gt;
    local data = {}&lt;br /&gt;
    for i, v in ipairs(frame.args) do data[i] = parse(v, i) end&lt;br /&gt;
 &lt;br /&gt;
    order = strSplit(order, &amp;#039;%s*,%s*&amp;#039;)&lt;br /&gt;
    nsort = strSplit(nsort, &amp;#039;%s*,%s*&amp;#039;)&lt;br /&gt;
    desc = strSplit(desc, &amp;#039;%s*,%s*&amp;#039;)&lt;br /&gt;
    hidden = strSplit(hidden, &amp;#039;%s*,%s*&amp;#039;)&lt;br /&gt;
    rowheader = strSplit(rowheader, &amp;#039;%s*,%s*&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
    for i, v in ipairs(order) do order[i] = tonumber(v) end&lt;br /&gt;
    for i, v in ipairs(nsort) do nsortLookup[tonumber(v) or -1] = true end&lt;br /&gt;
    for i, v in ipairs(desc) do descLookup[tonumber(v) or -1] = true end&lt;br /&gt;
    for i, v in ipairs(hidden) do hiddenLookup[tonumber(v) or -1] = true end&lt;br /&gt;
    for i, v in ipairs(rowheader) do rowHeading[tonumber(v) or -1] = true end&lt;br /&gt;
&lt;br /&gt;
    --Sorting comparator function.&lt;br /&gt;
    local sortFunc = function(a, b)&lt;br /&gt;
        local ad, bd = a.data, b.data&lt;br /&gt;
        for i = 1, #order do&lt;br /&gt;
            local index = order[i]&lt;br /&gt;
            local ai, bi = ad[index], bd[index]&lt;br /&gt;
            if nsortLookup[index] then&lt;br /&gt;
                -- Numeric sort. Find the first occurrence of a number and use it. Decimal points are allowed. Scientific notation not supported.&lt;br /&gt;
                ai = tonumber( (ai:find(&amp;#039;.&amp;#039;, 1, true) and ai:match(&amp;#039;[+-]?%d*%.%d+&amp;#039;) or ai:match(&amp;#039;[+-]?%d+&amp;#039;)) or 0 )&lt;br /&gt;
                bi = tonumber( (bi:find(&amp;#039;.&amp;#039;, 1, true) and bi:match(&amp;#039;[+-]?%d*%.%d+&amp;#039;) or bi:match(&amp;#039;[+-]?%d+&amp;#039;)) or 0 )&lt;br /&gt;
            end&lt;br /&gt;
            if ai ~= bi then&lt;br /&gt;
                if descLookup[index] then return ai &amp;gt; bi else return ai &amp;lt; bi end&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
        return a.key &amp;lt; b.key&lt;br /&gt;
    end&lt;br /&gt;
    table.sort(data, sortFunc)&lt;br /&gt;
 &lt;br /&gt;
    -- Write the sorted data to the HTML output&lt;br /&gt;
    for i, v in ipairs(data) do writeHtml(v.css, v.data, nil) end&lt;br /&gt;
 &lt;br /&gt;
    -- Write the footer&lt;br /&gt;
    if footer then&lt;br /&gt;
        local footerData = parse(footer)&lt;br /&gt;
        writeHtml(footerData.css, footerData.data, &amp;#039;footer&amp;#039;)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return tostring(html)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return _module&lt;/div&gt;</summary>
		<author><name>imported&gt;JohnFromPinckney</name></author>
	</entry>
</feed>