Difference between revisions of "Module:SortList"

From Blaseball Wiki

(Undo revision 30966 by OliverIsARobot (talk))
Tag: Undo
m
Tag: 2017 source edit
 
(9 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
function p.asc(frame)
 
function p.asc(frame)
     items = splitLine( frame.args[1] );
+
     items = splitLine( frame.args[2], frame.args[1] );
 
     table.sort( items );
 
     table.sort( items );
     return table.concat( items, "\n" );     
+
     return table.concat( items, "" );     
 +
end
 +
function p.desc(frame)
 +
    items = splitLine( frame.args[2], frame.args[1] );
 +
    table.sort( items, function (a, b) return a >= b end );
 +
    return table.concat( items, "" );
 
end
 
end
  
function p.desc(frame)
+
function p.reverse(frame)
     items = splitLine( frame.args[1] );
+
     items = splitLine( frame.args[2], frame.args[1] );
     table.sort( items, function (a, b) return a > b end );
+
     local n = #items
     return table.concat( items, "\n" );
+
    local i = 1
 +
    while i < n do
 +
        items[i],items[n] = items[n],items[i]
 +
        i = i + 1
 +
        n = n - 1
 +
    end
 +
     return table.concat( items, "" );
 
end
 
end
  
function splitLine( text )
+
function splitLine( separator, text )
     return mw.text.split( text, "\n", true );  
+
separator = separator or "\n"
 +
     return mw.text.split( text, separator, true );
 
end
 
end
  

Latest revision as of 02:08, 6 October 2020

local p = {}

function p.asc(frame)

   items = splitLine( frame.args[2], frame.args[1] );
   table.sort( items );
   return table.concat( items, "" );    

end function p.desc(frame)

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

end

function p.reverse(frame)

   items = splitLine( frame.args[2], frame.args[1] );
   local n = #items
   local i = 1
   while i < n do
       items[i],items[n] = items[n],items[i]
       i = i + 1
       n = n - 1
   end
   return table.concat( items, "" );

end

function splitLine( separator, text ) separator = separator or "\n"

   return mw.text.split( text, separator, true );

end


return p