Template:Number table sorting/doc

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 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:
 * 1) When numbers are followed by some text
 * 2) 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
 * : To display some text before the number, use some prefix
 * : For unformatted output, use no
 * : To display the sort key, use yes.
 * : To display the sort key, use yes.
 * : To display the sort key, use yes.
 * : To display the sort key, use 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.

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  parameter) does not affect the sort order.

Examples
→

For text which follows a number,  displays   with a numerical sort key of. 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.