Difference between revisions of "Template:Number table sorting/doc"
[unchecked revision] | [unchecked revision] |
(tidied) |
Aeverinezinn (talk | contribs) m (26 revisions from wikipedia:Template:Number table sorting/doc) |
||
(34 intermediate revisions by 20 users not shown) | |||
Line 1: | Line 1: | ||
{{Documentation subpage}} | {{Documentation subpage}} | ||
{{Template shortcut|Nts}} | {{Template shortcut|Nts}} | ||
+ | {{mbox | ||
+ | | type = notice | ||
+ | | text = Note. Adding <code>data-sort-type="number"</code> to the relevant column header solves many numerical sorting problems. See [[Help:Sorting#Forcing a column to have a particular data type|Help:Sorting]]. | ||
+ | }} | ||
+ | This template can be useful when building a [[Help:Sorting|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 −10<sup>308</sup> and 10<sup>308</sup>. Numbers outside this range will sort above or below other numbers depending on sign. See the [[#Limitations|Limitations]] section below. | |
− | |||
− | Currently, the template works for numbers | ||
By default the output is displayed using thousands separators. To display numbers as entered, use {{para|format|no}}. | By default the output is displayed using thousands separators. To display numbers as entered, use {{para|format|no}}. | ||
− | The template generates a hidden "sort key" in the [[HTML]] markup which forces the [[ | + | The template generates a hidden "sort key" in the [[HTML]] markup which forces the [[JavaScript]] [[sorting algorithm]] to sort the column alphabetically. |
===Purpose=== | ===Purpose=== | ||
Line 21: | Line 24: | ||
* <code>prefix</code>: To display some text before the number, use {{para|prefix|''some prefix''}} | * <code>prefix</code>: To display some text before the number, use {{para|prefix|''some prefix''}} | ||
*: <code><nowiki>{{nts|123456789.00123|prefix=approx.&nbsp;}}</nowiki></code> → {{nts|123456789.00123|prefix=approx. }} | *: <code><nowiki>{{nts|123456789.00123|prefix=approx.&nbsp;}}</nowiki></code> → {{nts|123456789.00123|prefix=approx. }} | ||
− | * <code>format</code>: | + | * <code>format</code>: For unformatted output, use {{para|format|no}} |
*: <code><nowiki>{{nts|123456789.00123}}</nowiki></code> → {{nts|123456789.00123}} | *: <code><nowiki>{{nts|123456789.00123}}</nowiki></code> → {{nts|123456789.00123}} | ||
*: <code><nowiki>{{nts|123456789.00123|format=no}}</nowiki></code> → {{nts|123456789.00123|format=no}} | *: <code><nowiki>{{nts|123456789.00123|format=no}}</nowiki></code> → {{nts|123456789.00123|format=no}} | ||
* <code>debug</code>: To display the sort key, use {{para|debug|yes}}. | * <code>debug</code>: To display the sort key, use {{para|debug|yes}}. | ||
− | *: <code><nowiki>{{nts|123456789.00123||debug|yes}}</nowiki></code> → {{nts|123456789.00123|debug=yes}} | + | *: <code><nowiki>{{nts|123456789.00123|debug=yes}}</nowiki></code> → {{nts|123456789.00123|debug=yes}} |
+ | *: <code><nowiki>{{nts|-123456789.00123|debug=yes}}</nowiki></code> → {{nts|-123456789.00123|debug=yes}} | ||
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. | 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. | ||
+ | |||
+ | {{TemplateDataHeader}} | ||
+ | <templatedata> | ||
+ | { | ||
+ | "description": "", | ||
+ | "params": { | ||
+ | "1": { | ||
+ | "label": "Number", | ||
+ | "type": "number", | ||
+ | "required": true, | ||
+ | "description": "Your number" | ||
+ | }, | ||
+ | "format": { | ||
+ | "label": "Format output?", | ||
+ | "type": "string", | ||
+ | "required": false, | ||
+ | "description": "If you do not wish the output to be formatted (i.e. separated by thousand separators), please put \"no\" in this field. (Without quotation marks.)" | ||
+ | }, | ||
+ | "debug": { | ||
+ | "label": "Debug", | ||
+ | "type": "string", | ||
+ | "required": false, | ||
+ | "description": "If set to \"yes\", forces output to include debug data" | ||
+ | }, | ||
+ | "prefix": { | ||
+ | "label": "Prefix", | ||
+ | "type": "string", | ||
+ | "required": false, | ||
+ | "description": "The prefix to be displayed before the number. E.g. \"Appox.\" or \"$\"" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </templatedata> | ||
+ | |||
+ | ==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<sup>−308</sup> and 10<sup>308</sup> 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 10<sup>14</sup>. | ||
+ | *For numbers between −10<sup>−308</sup> and −10<sup>308</sup> 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 10<sup>14</sup>. | ||
+ | *The sort key for 0 is 5000000000000000000. | ||
+ | |||
+ | ;Numbers out of range | ||
+ | *Numbers larger than 10<sup>308</sup> are assigned the sort key 9000000000000000000. | ||
+ | *Numbers smaller than −10<sup>308</sup> are assigned the sort key 1000000000000000000. | ||
+ | *Numbers between 10<sup>−308</sup> and 0 or between 0 and −10<sup>−308</sup> are assigned the sort key 5000000000000000000. | ||
==Limitations== | ==Limitations== | ||
− | *Any subset of numbers larger than | + | *Any subset of numbers larger than 10<sup>308</sup> are sorted together. |
− | *Any subset of numbers | + | *Any subset of numbers smaller than −10<sup>308</sup> are sorted together. |
+ | *Any subset of numbers between 10<sup>−308</sup> and −10<sup>−308</sup> are sorted together. | ||
*If a non-numeric value is given as the first unnamed parameter the results are undefined. | *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 <code>prefix</code> | + | *A prefix (using the <code>prefix</code> parameter) does not affect the sort order. |
==Examples== | ==Examples== | ||
− | <code><nowiki>{{nts|123456789.00123}}</nowiki></code> → <code><nowiki>< | + | <code><nowiki>{{nts|123456789.00123}}</nowiki></code> → {{nts|123456789.00123|debug=yes}} |
+ | |||
+ | For text which follows a number, <code><nowiki>{{nts|123,456}}</nowiki> as of 2012</code> displays <code>123,456 as of 2012</code> with a numerical sort key of <code>123456</code>. 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. | Below are more examples, some of which illustrate the limitations listed above. | ||
Line 51: | Line 104: | ||
|- | |- | ||
| <code><nowiki>{{nts|between 2 and 3|debug=yes}}</nowiki></code> || {{nts|between 2 and 3|debug=yes}} | | <code><nowiki>{{nts|between 2 and 3|debug=yes}}</nowiki></code> || {{nts|between 2 and 3|debug=yes}} | ||
+ | |- | ||
+ | | <code><nowiki>{{nts|between 2 and 3|debug=no}}</nowiki></code> || {{nts|between 2 and 3|debug=no}} | ||
|- | |- | ||
| <code><nowiki>{{nts|10|prefix=approx.{{sp}}|debug=yes}}</nowiki></code> || {{nts|10|prefix=approx.{{sp}}|debug=yes}} | | <code><nowiki>{{nts|10|prefix=approx.{{sp}}|debug=yes}}</nowiki></code> || {{nts|10|prefix=approx.{{sp}}|debug=yes}} | ||
Line 129: | Line 184: | ||
==See also== | ==See also== | ||
* {{tl|Ntsh}} - same as this template, but does not display (hides) the number | * {{tl|Ntsh}} - same as this template, but does not display (hides) the number | ||
− | * {{tl| | + | * {{tl|val}} - displays numbers and quantities with various formatting options |
− | |||
* {{tl|Convert}} - has a sortable option | * {{tl|Convert}} - has a sortable option | ||
* {{tl|Dts}} - for sorting dates | * {{tl|Dts}} - for sorting dates | ||
− | * {{tl| | + | * {{tl|Sort}} |
+ | * {{tl|Hidden sort key}} | ||
<includeonly> | <includeonly> | ||
[[Category:Sorting templates|Nts]] | [[Category:Sorting templates|Nts]] | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</includeonly> | </includeonly> |
Latest revision as of 04:21, 7 March 2016
This is a documentation subpage for Template:Number table sorting. It contains usage information, categories and other content that is not part of the original template page. |
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[edit]
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[edit]
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. 123,456,789.00123
-
-
format
: For unformatted output, use|format=no
-
{{nts|123456789.00123}}
→ 123,456,789.00123 -
{{nts|123456789.00123|format=no}}
→ 123456789.00123
-
-
debug
: To display the sort key, use|debug=yes
.-
{{nts|123456789.00123|debug=yes}}
→ 7008123456789001230♠123,456,789.00123 -
{{nts|-123456789.00123|debug=yes}}
→ 2991876543210998770♠−123,456,789.00123
-
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.
Number table sorting
Parameter | Description | Type | Status | |
---|---|---|---|---|
Number | 1 | Your number
| Number | required |
Format output? | format | If you do not wish the output to be formatted (i.e. separated by thousand separators), please put "no" in this field. (Without quotation marks.)
| String | optional |
Debug | debug | If set to "yes", forces output to include debug data
| String | optional |
Prefix | prefix | The prefix to be displayed before the number. E.g. "Appox." or "$"
| String | optional |
Sort key[edit]
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[edit]
- 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[edit]
{{nts|123456789.00123}}
→ 7008123456789001230♠123,456,789.00123
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}} |
||
{{nts|between 2 and 3|debug=no}} |
||
{{nts|10|prefix=approx.{{sp}}|debug=yes}} |
7001100000000000000♠approx. 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}} |
2983796543210987654♠−2.0345678901235×10 16 | |
{{nts|123.456e3|debug=yes}} |
7005123456000000000♠1.23456×10 5 | |
{{nts|123.456e2|debug=yes}} |
7004123456000000000♠1.23456×10 4 | |
{{nts|20345678901234567.12345678|debug=yes}} |
7016203456789012345♠20,345,678,901,234,567.12345678 | |
{{nts|100345678901234567.12345678|debug=yes}} |
7017100345678901234♠100,345,678,901,234,567.12345678 | |
{{nts|1234567890123456.12345678|debug=yes}} |
7015123456789012345♠1,234,567,890,123,456.12345678 | |
{{nts|234567890123456.12345678|debug=yes}} |
7014234567890123456♠234,567,890,123,456.12345678 | |
{{nts|34567890123456.12345678|debug=yes}} |
7013345678901234561♠34,567,890,123,456.12345678 | |
{{nts|4567890123456.12345678|debug=yes}} |
7012456789012345612♠4,567,890,123,456.12345678 | |
{{nts|567890123456.12345678|debug=yes}} |
7011567890123456123♠567,890,123,456.12345678 | |
{{nts|67890123456.12345678|debug=yes}} |
7010678901234561234♠67,890,123,456.12345678 | |
{{nts|7890123456.12345678|debug=yes}} |
7009789012345612345♠7,890,123,456.12345678 | |
{{nts|890123456.12345678|debug=yes}} |
7008890123456123456♠890,123,456.12345678 | |
{{nts|90123456.12345678|debug=yes}} |
7007901234561234567♠90,123,456.12345678 | |
{{nts|0123456.12345678|debug=yes}} |
7005123456123456780♠0,123,456.12345678 | |
{{nts|123456.12345678|debug=yes}} |
7005123456123456780♠123,456.12345678 | |
{{nts|23456.12345678|debug=yes}} |
7004234561234567799♠23,456.12345678 | |
{{nts|3456.12345678|debug=yes}} |
7003345612345678000♠3,456.12345678 | |
{{nts|456.12345678|debug=yes}} |
7002456123456780000♠456.12345678 | |
{{nts|56.12345678|debug=yes}} |
7001561234567800000♠56.12345678 | |
{{nts|6.12345678|debug=yes}} |
7000612345678000000♠6.12345678 | |
{{nts|.12345678|debug=yes}} |
6999123456780000000♠.12345678 | |
{{nts|0.12345678|debug=yes}} |
6999123456780000000♠0.12345678 | |
{{nts|0.02345678|debug=yes}} |
6998234567800000000♠0.02345678 | |
{{nts|0.00345678|debug=yes}} |
6997345678000000000♠0.00345678 | |
{{nts|0.00045678|debug=yes}} |
6996456780000000000♠0.00045678 | |
{{nts|0.00005678|debug=yes}} |
6995567800000000000♠0.00005678 | |
{{nts|0.00000678|debug=yes}} |
6994678000000000000♠0.00000678 | |
{{nts|0.00000078|debug=yes}} |
6993780000000000000♠0.00000078 | |
{{nts|0.00000008|debug=yes}} |
6992800000000000000♠0.00000008 | |
{{nts|.00000008|debug=yes}} |
6992800000000000000♠.00000008 | |
{{nts|0|debug=yes}} |
5000000000000000000♠0 |
See also[edit]
- {{Ntsh}} - same as this template, but does not display (hides) the number
- {{val}} - displays numbers and quantities with various formatting options
- {{Convert}} - has a sortable option
- {{Dts}} - for sorting dates
- {{Sort}}
- {{Hidden sort key}}