localgetArgs=require('Module:Arguments').getArgslocalp={}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")thenreturn"";endreturnmw.text.encode(sortkey)endfunctionp.encode(frame)localargs=getArgs(frame);returnp._encode(args[1]or"")endlocalfunctionvalid_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)==nilthenreturntrueendendfunctionp._sortKeyForNumber(value)ifnotvalid_number(value)thenifvalue<0thensortkey='1000000000000000000'elsesortkey='9000000000000000000'endelseifvalue==0thensortkey='5000000000000000000'elselocalmag=math.floor(math.log10(math.abs(value))+1e-14)localprefixifvalue>0thenprefix=7000+magelseprefix=2999-magvalue=value+10^(mag+1)endsortkey=string.format('%d',prefix)..string.format('%015.0f',math.floor(value*10^(math.min(28,14-mag))))endreturnsortkey;endfunctionp.sortKeyForNumber(frame)localargs=getArgs(frame);returnp._sortKeyForNumber(args[1]or"")endreturnp