Sjabloon:Number table sorting
Note. Adding data-sort-type="number" to the relevant column header solves many numerical sorting problems. See Help:Sorting. |
This template can be useful when building a sortable table in which a column contains both numbers and text. This template should be applied to every number in the column.
Currently, the template works for numbers between −10308 and 10308. Numbers outside this range will sort above or below other numbers depending on sign. See the Limitations section below.
By default the output is displayed using thousands separators. To display numbers as entered, use |format=no
.
The template generates a hidden "sort key" in the HTML markup which forces the JavaScript sorting algorithm to sort the column alphabetically.
Purpose
There are at two cases where this template is useful:
- When numbers are followed by some text
- When numbers are preceded by some text other than a currency symbol.
Parameters
The template has only one required parameter: a real number. These examples use the name of the redirect, {{nts}}, for brevity.
- Optional parameters
prefix
: To display some text before the number, use|prefix=some prefix
{{nts|123456789.00123|prefix=approx. }}
→ approx. 12.345.678.900.123
format
: For unformatted output, use|format=no
{{nts|123456789.00123}}
→ 12.345.678.900.123{{nts|123456789.00123|format=no}}
→ 12345678900123
debug
: To display the sort key, use|debug=yes
.{{nts|123456789.00123|debug=yes}}
→ 701312345678900123012.345.678.900.123{{nts|-123456789.00123|debug=yes}}
→ 2986876543210998770−12.345.678.900.123
Apart from the added thousands separators the numbers are formatted as supplied (scientific notation or not, leading and trailing zeros, and a zero before the decimal point or not). This formatting does not affect the sorted order except for numbers not satisfying the limitations mentioned below.
Sort key
The sort key is a nineteen-digit number. For numbers within range the first four digits are determined by the number's sign and order of magnitude and the next fifteen digits are determined by the number's sign and significand.
- Numbers within range
- For numbers between 10−308 and 10308 the first four digits are calculated by adding 7000 to the order of magnitude and the next fifteen digits are calculated by multiplying the significand by 1014.
- For numbers between −10−308 and −10308 the first four digits are calculated by subtracting the order of magnitude from 2999 and the next fifteen digits are calculated by subtracting the significand from 10 multiplying the difference by 1014.
- The sort key for 0 is 5000000000000000000.
- Numbers out of range
- Numbers larger than 10308 are assigned the sort key 9000000000000000000.
- Numbers smaller than −10308 are assigned the sort key 1000000000000000000.
- Numbers between 10−308 and 0 or between 0 and −10−308 are assigned the sort key 5000000000000000000.
Limitations
- Any subset of numbers larger than 10308 are sorted together.
- Any subset of numbers smaller than −10308 are sorted together.
- Any subset of numbers between 10−308 and −10−308 are sorted together.
- If a non-numeric value is given as the first unnamed parameter the results are undefined.
- The hyphen minus sign is converted into a true minus sign; note, though, that this means no more than 12 significant figures are possible.
- A prefix (using the
prefix
parameter) does not affect the sort order.
Examples
{{nts|123456789.00123}}
→ 701312345678900123012.345.678.900.123
For text which follows a number, {{nts|123,456}} as of 2012
displays 123,456 as of 2012
with a numerical sort key of 123456
. This forces numerical sorting in the cell using this value instead of the default alphabetical sorting.
Below are more examples, some of which illustrate the limitations listed above.
markup | sortkey and number | error spillover |
---|---|---|
{{nts|debug=yes}} |
||
{{nts||debug=yes}} |
||
{{nts|between 2 and 3|debug=yes}} |
Error: between 2 and 3 This is not a valid number. Please refer to the documentation at {{number table sorting}} for correct input. | |
{{nts|10|prefix=approx.{{sp}}|debug=yes}} |
7001100000000000000approx. 10 | |
{{nts|-5|debug=yes}} |
2999500000000000000−5 | |
{{nts|-4|debug=yes}} |
2999600000000000000−4 | |
{{nts|-73|debug=yes}} |
2998270000000000000−73 | |
{{nts|-67|debug=yes}} |
2998330000000000000−67 | |
{{nts|-20345678901234567.12345678|debug=yes}} |
2975796543210987654−2,0345678901235×10 24 | |
{{nts|123.456e3|debug=yes}} |
70081234560000000001,23456×10 8 | |
{{nts|123.456e2|debug=yes}} |
70071234560000000001,23456×10 7 | |
{{nts|20345678901234567.12345678|debug=yes}} |
70242034567890123452.034.567.890.123.456.712.345.678 | |
{{nts|100345678901234567.12345678|debug=yes}} |
702510034567890123410.034.567.890.123.456.712.345.678 | |
{{nts|1234567890123456.12345678|debug=yes}} |
7023123456789012345123.456.789.012.345.612.345.678 | |
{{nts|234567890123456.12345678|debug=yes}} |
702223456789012345623.456.789.012.345.612.345.678 | |
{{nts|34567890123456.12345678|debug=yes}} |
70213456789012345613.456.789.012.345.612.345.678 | |
{{nts|4567890123456.12345678|debug=yes}} |
7020456789012345612456.789.012.345.612.345.678 | |
{{nts|567890123456.12345678|debug=yes}} |
701956789012345612356.789.012.345.612.345.678 | |
{{nts|67890123456.12345678|debug=yes}} |
70186789012345612346.789.012.345.612.345.678 | |
{{nts|7890123456.12345678|debug=yes}} |
7017789012345612345789.012.345.612.345.678 | |
{{nts|890123456.12345678|debug=yes}} |
701689012345612345689.012.345.612.345.678 | |
{{nts|90123456.12345678|debug=yes}} |
70159012345612345679.012.345.612.345.678 | |
{{nts|0123456.12345678|debug=yes}} |
7013123456123456780012.345.612.345.678 | |
{{nts|123456.12345678|debug=yes}} |
701312345612345678012.345.612.345.678 | |
{{nts|23456.12345678|debug=yes}} |
70122345612345678002.345.612.345.678 | |
{{nts|3456.12345678|debug=yes}} |
7011345612345678000345.612.345.678 | |
{{nts|456.12345678|debug=yes}} |
701045612345678000045.612.345.678 | |
{{nts|56.12345678|debug=yes}} |
70095612345678000005.612.345.678 | |
{{nts|6.12345678|debug=yes}} |
7008612345678000000612.345.678 | |
{{nts|.12345678|debug=yes}} |
700712345678000000012.345.678 | |
{{nts|0.12345678|debug=yes}} |
7007123456780000000012.345.678 | |
{{nts|0.02345678|debug=yes}} |
7006234567800000000002.345.678 | |
{{nts|0.00345678|debug=yes}} |
7005345678000000000000.345.678 | |
{{nts|0.00045678|debug=yes}} |
7004456780000000000000.045.678 | |
{{nts|0.00005678|debug=yes}} |
7003567800000000000000.005.678 | |
{{nts|0.00000678|debug=yes}} |
7002678000000000000000.000.678 | |
{{nts|0.00000078|debug=yes}} |
7001780000000000000000.000.078 | |
{{nts|0.00000008|debug=yes}} |
7000800000000000000000.000.008 | |
{{nts|.00000008|debug=yes}} |
700080000000000000000.000.008 | |
{{nts|0|debug=yes}} |
50000000000000000000 |
See also
- {{Ntc}} - creates a center aligned sortable numeric table cell
- {{Ntl}} - creates a left aligned sortable numeric table cell
- {{Ntr}} - creates a right aligned sortable numeric table cell
- {{Ntsh}} - same as this template, but does not display (hides) the number
- {{Ntsc}} - for converted units, sorts by the first number
- {{Ntss}} - same as Nts, but allows for the number to be scaled
- {{Convert}} - has a sortable option
- {{Dts}} - for sorting dates
- {{Sort}}
- {{Hidden sort key}}
The above documentation is transcluded from Sjabloon:Number table sorting/doc. (edit | history) Editors can experiment in this template's sandbox (create) and testcases (create) pages. Please add categories and interwikis to the /doc subpage. Subpages of this template. |