Help:Sorting: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
→‎Examples: rm some, add link
Line 106: Line 106:
*If at some point (i.e., after possible previous sorting) the form <nowiki>[[YYYY-MM-DD]]</nowiki> or <nowiki>[[YYYY]]-MM</nowiki> is at the top, sorting works fine: alphabetically, with <nowiki>[[YYYY]]</nowiki> positioned between the first day of the year concerned and the previous day.
*If at some point (i.e., after possible previous sorting) the form <nowiki>[[YYYY-MM-DD]]</nowiki> or <nowiki>[[YYYY]]-MM</nowiki> is at the top, sorting works fine: alphabetically, with <nowiki>[[YYYY]]</nowiki> positioned between the first day of the year concerned and the previous day.
*If at some point the form <nowiki>[[YYYY]]</nowiki> is at the top, sorting is numerical; in this case, after toggling between ascending and descending there is no proper sorting ''within'' each year (because [http://www.devguru.com/technologies/ecmascript/QuickRef/parsefloat.html parsefloat] is applied, finding the first number in the string, and basing sorting on that only). When this happens it sometimes helps to toggle twice between ascending and descending order, namely in the case that after toggling once, this form is ''not'' at the top.
*If at some point the form <nowiki>[[YYYY]]</nowiki> is at the top, sorting is numerical; in this case, after toggling between ascending and descending there is no proper sorting ''within'' each year (because [http://www.devguru.com/technologies/ecmascript/QuickRef/parsefloat.html parsefloat] is applied, finding the first number in the string, and basing sorting on that only). When this happens it sometimes helps to toggle twice between ascending and descending order, namely in the case that after toggling once, this form is ''not'' at the top.

Example:
{|class=sortable
!date
|-
| [[-0099-07-13]]
|-
| [[-0062-09-23]]
|-
| [[2006-12-03]]
|-
| [[2006-12-04]]
|}


==Examples==
==Examples==

Revision as of 12:36, 3 December 2006

Tables can now be made sortable via JavaScript with class="sortable" using [1]. The table gets arrows in each of its header cells. Clicking them will cause the table rows to sort based on the selected column, in ascending order first, and subsequently toggling between ascending and descending order. Links and other wiki-markup are not possible in headers.

Sorting modes

The sorting modes are:

  • string
    • criterion: the first element is not of type numeric, date or currency
    • order: ASCII - partial list showing the order: !"#$%&'()*+,-./09:;<=>?@AZ[\]^_'az{|}~ (a blank space comes before every other character; an nbsp code counts as a space)
      • for numbers the most relevant are: +,-.0123456789Ee
  • numeric
    • criterion: the first element consists of just digits, or digits and a decimal point; not recognized as numeric are:
    • order: if the string starts with a number (where spaces and nbsp's at the start are ignored) the order is numeric according to the first number in the string (parsefloat is applied); if it does not, the element is positioned like 0; negative numbers and numbers in scientific notation are properly sorted (if, as said, the first element is not like that), but numbers with comma or space separators are not: they are sorted like the number before the first comma or space
  • date (see also below)
    • criterion: the first element is of the form dd-dd-dddd or dd-dd-dd
    • order: the string abcdefghij of length 10 is positioned as ghijdeab, the string abcdefghijk of any other length as 19ghdeab if gh>=50 (string comparison) and 20ghdeab otherwise (i.e., the assumed format is DD-MM-YYYY or DD-MM-YY)
  • currency

The sorting mode is determined by the table element that is currently in the first row below the header. Thus it may change after sorting, which can give a cycle of four or even more instead of two.

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 "&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, 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).

Within a column, either all or no numbers in the range 0 - 1 should have a zero before the decimal point.

Making variable-length numbers with thousands separators sortable

This section deals with the case where the width is not fixed or not known, as in the case where the number depends on parameters or templates, and/or is the result of a computation.

Using an invisible padding character

We cannot use variable padleft with the nbsp code, because &, the first character of the code, is used for padding, even if the code is put in a template. Therefore a template is used, e.g. Template:Ls12 or Template:Lsc12. The latter also provides thousands separators, and the pluses necessary in the range 0 - 1000 (see above).

Padding with zeros

Example:

  • 000156

Formatnum can be combined with padleft:

Integer:

{{formatnum:{{padleft:299792458|16|0}}}} gives:

  • 0,000,000,299,792,458

Real:

{{formatnum:{{padleft:{{#expr:((299792458.056 - .5) round 0)}}|16|0}}}}.{{padleft:{{#expr:(1000000*(299792458.056 - ((299792458.056 - .5) round 0))) round 0}}|6|0}} gives:

  • 0,000,000,299,792,458.056000

Sorting with negative numbers

If a column contains only a few negative numbers we can preserve the correspondence between alphabetic and numeric order by putting the minuses at the position of the right-most plus sign in the column, and inserting a number of nbsp codes after the minus sign, the closer to zero the more.

A column of plain numbers, partly negative, sorts as follows:

  • if the first number is positive, sorting alternates between ascending numeric order and descending alphabetic order
  • if the first number is negative, sorting first gives ascending alphabetic order, and then alternates again between ascending numeric order and descending alphabetic order (starting with the latter)
alphabetic sorting mode plain numbers, starting with positive plain numbers, starting with negative
          +6 6 -5
          +7 7 7
   1,048,576 1048576 1048576
       1,234 1234 1234
        +123 123 123
1,073,741,824 1073741824 1073741824
      65,536 65536 65536
  67,108,864 67108864 67108864
17,179,869,184 17179869184 17179869184
          -  3 -3 -3
           - 4 -4 -4
            -5 -5 6

Dates

For dates the sorting mode is based on the rendered date format. Unfortunately none of the standard formats for the date formatting feature matches either of the formats giving sorting mode "date". Thus if dates are entered in one of these standard formats the sorting mode is "string"; only for format YYYY-MM-DD string sorting corresponds to chronological sorting.

Thus the cases where we get chronological sorting include:

  • the wikitext uses any standard format for the date formatting feature, and the date format set in the preferences is [[YYYY-MM-DD]]
  • the wikitext uses format [[YYYY-MM-DD]], and the date format in the preferences is not set (this includes anon users) or set to "no preference", or set to [[YYYY-MM-DD]]
  • format DD-MM-[[YYYY]] (not recommended, except on wikis which abandon the date formatting feature in favor of this format)

Thus, users setting the preference [[YYYY-MM-DD]] and using as editor the wikitext format [[YYYY-MM-DD]] make all tables using the standard formats for the date formatting feature sortable for themselves, while making their tables sortable both for people with "no preference" and people with preference [[YYYY-MM-DD]].

If a table column contains also, or only, incomplete dates of the form [[YYYY]]-MM, this does not give complications: such an incomplete date is positioned alphabetically between the first day of the month concerned and the previous day.

If a table column contains only incomplete dates of the form [[YYYY]], this does not give complications either.

If a table column contains also, but not only, incomplete dates of the form YYYY, we have to distinguish two cases:

  • If at some point (i.e., after possible previous sorting) the form [[YYYY-MM-DD]] or [[YYYY]]-MM is at the top, sorting works fine: alphabetically, with [[YYYY]] positioned between the first day of the year concerned and the previous day.
  • If at some point the form [[YYYY]] is at the top, sorting is numerical; in this case, after toggling between ascending and descending there is no proper sorting within each year (because parsefloat is applied, finding the first number in the string, and basing sorting on that only). When this happens it sometimes helps to toggle twice between ascending and descending order, namely in the case that after toggling once, this form is not at the top.

Example:

date
-0099-07-13
-0062-09-23
2006-12-03
2006-12-04

Examples

See also

Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences
Editing
Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URL · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · Lists · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Magic words · System message · Substitution · Array · Calculation · Transclusion
Others
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors
Other languages: