Module:Sort list

From Wikibooks, open books for an open world
Jump to navigation Jump to search
local p = {}

function p.asc(frame)
    items = splitLine( frame.args[1] );
    table.sort( items );
    return table.concat( items, "\n" );    
end

function p.asc_link(frame)
    items = splitLine( frame.args[2] );
    table.sort( items );
    for i, item in ipairs(items) do
    	if item ~= nil and item ~= '' then 
    		items[i] = "[[" .. frame.args[1] .. "/" .. removeAsterisk( item ) .. "|" .. removeAsterisk( item ) .. "]]";
    		items[i] = "* " .. items[i];
    	end
	end
    return table.concat( items, "\n" );    
end

function p.desc(frame)
    items = splitLine( frame.args[1] );
    table.sort( items, function (a, b) return a > b end );
    return table.concat( items, "\n" );
end

function splitLine( text )
    return mw.text.split( text, "\n", true );    
end

function removeAsterisk( text )
	t = mw.text.nowiki( text );
	return string.gsub(text, "%* ", "");
end


return p