Modulu:Sortkey
Itxura
Documentation for this module may be created atModulu:Sortkey/dok
localgetArgs=require('Module:Arguments').getArgs
localp={}
functionp._encode(sortkey)
-- Protect against sortkey nesting.
-- Example: {{sort|{{dts|2013|07|07}}|{{dts|1990|12|01}}}}
ifstring.find(sortkey,"sortkey")orstring.find(sortkey,"data-sort-value")then
return"";
end
returnmw.text.encode(sortkey)
end
functionp.encode(frame)
localargs=getArgs(frame);
returnp._encode(args[1]or"")
end
localfunctionvalid_number(num)
-- Return true if num is a valid number.
-- In Scribunto (different from some standard Lua), when expressed as a string,
-- overflow or other problems are indicated with text like "inf" or "nan"
-- which are regarded as invalid here (each contains "n" ).
iftype(num)=='number'andtostring(num):find('n',1,true)==nilthen
returntrue
end
end
functionp._sortKeyForNumber(value)
ifnotvalid_number(value)then
ifvalue<0then
sortkey='1000000000000000000'
else
sortkey='9000000000000000000'
end
elseifvalue==0then
sortkey='5000000000000000000'
else
localmag=math.floor(math.log10(math.abs(value))+1e-14)
localprefix
ifvalue>0then
prefix=7000+mag
else
prefix=2999-mag
value=value+10^(mag+1)
end
sortkey=string.format('%d',prefix)..string.format('%015.0f',math.floor(value*10^(math.min(28,14-mag))))
end
returnsortkey;
end
functionp.sortKeyForNumber(frame)
localargs=getArgs(frame);
returnp._sortKeyForNumber(args[1]or"")
end
returnp