Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Usage  














Module:Sortkey






Afrikaans
Аԥсшәа
العربية
Արեւմտահայերէն

Asturianu
Авар
Azərbaycanca
Basa Bali

 / Bân-lâm-gú
Беларуская (тарашкевіца)

Bikol Central
Bosanski
Буряад
Català
Cebuano
Chi-Chewa
Dagbanli
الدارجة
Eesti
Euskara
فارسی
Gaeilge

/Hak-kâ-ngî

Hausa
Հայերեն
Ilokano
Bahasa Indonesia
Íslenska

Ladin
Lëtzebuergesch
Magyar
Madhurâ
ि

Māori
Bahasa Melayu
 / Mìng-dĕ̤ng-nḡ
Монгол


Oʻzbekcha / ўзбекча

Pangasinan
پښتو
Português
Română
Русский
Gagana Samoa
Scots

Simple English
سنڌي
Slovenščina
کوردی
Српски / srpski
Tagalog
ி
Taqbaylit
 

Türkçe
Українська
اردو
Tiếng Vit



 

Edit links
 









Module
Talk
 

















Read
View source
View history
 








Tools
   


Actions  



Read
View source
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 





Permanently protected module

From Wikipedia, the free encyclopedia
 


This module sanitizes and escapes sortkeys so that they can be used inside the data-sort-value attribute of HTML tags in combination with table sorting. Use it when you cannot be certain that the provided sortkey input is a correct sortkey.

Usage

{{#invoke:Sortkey|escape|sortkey to sanitize}}

local getArgs = require('Module:Arguments').getArgs

local p = {}

function p._encode(sortkey)
 -- Protect against sortkey nesting.
 -- Example: {{sort|{{dts|2013|07|07}}|{{dts|1990|12|01}}}}
 if string.find(sortkey, "sortkey") or string.find(sortkey, "data-sort-value") then
  return "";
 end
    return mw.text.encode(sortkey)
end

function p.encode(frame)
 local args = getArgs(frame);
 return p._encode(args[1] or "")
end

local function valid_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").
 if type(num) == 'number' and tostring(num):find('n', 1, true) == nil then
  return true
 end
end

function p._sortKeyForNumber(value)
 local sortkey
 if not valid_number(value) then
  if value < 0 then
   sortkey = '1000000000000000000'
  else
   sortkey = '9000000000000000000'
  end
 elseif value == 0 then
  sortkey = '5000000000000000000'
 else
  local mag = math.floor(math.log10(math.abs(value)) + 1e-14)
  local prefix
  if value > 0 then
   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
 return sortkey;
end

function p.sortKeyForNumber(frame)
 local args = getArgs(frame);
 return p._sortKeyForNumber(args[1] or "")
end

return p

Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:Sortkey&oldid=1048391394"

Category: 
Modules for general use
Hidden category: 
Wikipedia template-protected modules
 



This page was last edited on 5 October 2021, at 18:54 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki