Content deleted Content added
No edit summary |
→Making variable-length numbers with thousands separators sortable: update from Meta of this section |
||
Line 32:
==Making variable-length numbers with thousands separators sortable==
For correct sorting of numbers with more than one comma, in one line of code in [http://meta.wikimedia.org/skins-1.5/common/wikibits.js ] etc. a "g" has to added: <nowiki>"a = a.replace(/,/g,'');"</nowiki> , i.e. use the [http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp:global global flag] when applying [http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:String:replace#Description replace]. In the meantime it can be fixed in [[MediaWiki:Common.js]], as has been done on Meta.
If a column consist of non-negative numbers, of which those >= 1000 have thousands separators, alphabetic sorting can be made to correspond with numeric sorting by leading [[w:Non-breaking space|"&nbsp;" codes]] which render as blank spaces (or with leading zeros) to equalize the number of characters before the explicit or implicit decimal separator.▼
Demo:
However, if at any time a number less than 1000 would be at the top, the sorting mode would be numeric, even with leading "&nbsp;" codes. Hence subsequent sorting would not work properly due to the thousands separators. One possible workaround is to force alphabetic sorting mode, by writing either all numbers, or just those in the range 0 - 1000, with a "+" in front. If all numbers get a plus, the absolute position of the plus has to be a non-increasing function of the number, e.g. the pluses are in the same absolute position, or in the same position relative to the first digit. If only the numbers in the range 0 - 1000 get a plus, the position of the first non-space character (plus or digit) has to be a non-increasing function of the number, so if there is a number in the range 1000 - 10000, pluses should be at most at the fifth position from the right to preserve the sorting order (+ 999 comes before 1,000).▼
{|class=sortable
!million test
|-
|123.4
|-
|2,500,000,000
|-
|300,000,000
|-
|3,000,000
|-
|5,000,000
|-
|2,000
|-
|4,000
|-
|9,999
|-
|800,000
|-
|900,000
|}
Other method:
▲If a column consist of non-negative numbers, of which those >= 1000 have thousands separators, alphabetic sorting can be made to correspond with numeric sorting by leading [[w:Non-breaking space|"&nbsp;" codes]] which render as blank spaces (or with leading zeros) to equalize the number of characters before the explicit or implicit decimal separator. (However, this does not seem to work in all browsers: it works in IE, but [[w:Talk:List_of_U.S._states_by_population#Sortable_Table|reportedly]] not in Firefox.)
However, if at any time a number less than 1000 would be at the top, the sorting mode would be numeric, even with leading "&nbsp;" codes. Hence subsequent sorting would not work properly due to the thousands separators. One possible workaround is to force alphabetic sorting mode, by writing either all numbers, or just those in the range 0 - 1000, with a "+" in front. ''(This no longer applies when a plus in the first number no longer prevents numeric sorting mode.)''
▲
Within a column, either all or no numbers in the range 0 - 1 should have a zero before the decimal point.
|