Jump to content

Help:Sorting: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
No edit summary
Line 29: Line 29:


===Using an invisible padding character===
===Using an invisible padding character===
We cannot use variable [[Help:Magic_words#Formatting|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. {{tim|ls12}} or {{tim|lsc12}}. The latter also provides thousands separators.
We cannot use variable [[Help:Magic_words#Formatting|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. {{tim|ls12}} or {{tim|lsc12}}. The latter also provides thousands separators, and the pluses necessary in the range 0 - 1000 (see above).


===Using a visible padding character===
===Using a visible padding character===

Revision as of 12:27, 2 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{|}~
      • 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).

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. The pluses can be put in the same absolute position, or in the same position relative to the first digit (e.g. with 0 or 1 spaces in between), but this has to be done consistently within each column to preserve the sorting order.

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).

Using a visible padding character

A visible padding character has to be one which in ASCII is before 0, or 0 itself, hence one of:

  • !"#$%&'()*+,-./0

and not confusing, hence not one of:

  • $%-.

and not one of the not working (not even with nowiki tags):

  • #'

Examples:

  • !!!156
  • """156
  • &&&156
  • (((156
  • )))156
  • +++156
  • ,,,156
  • ///156
  • 000156

Not working:

  • 156
  • 156

Formatnum can be combined with padleft:

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

  • 000,299,792,458.056

However, we want to pad to a given number of positions left of the decimal point, and not to a given total number if digits. For example 16 digits before the point and 6 after:

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

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.

Examples

The numbers have leading "&nbsp;" codes. Note that the density column has a cycle of 4.

Province Population Area (km²) Density GDP(PPS in mil. € 2003) GDP per cap. (in € 2003) demo
South Holland  3,453,000  2,860 1,207.3  95,868 27,825 1,207.3
a  2,583,900  2,660   971.4  65,295 27,169  ,971.4
Utrecht  1,159,200  1,356   854.9  38,355 33,148 ____3
Limburg  1,143,000  2,167   527.5  28,038 24,585 ___33
North Brabant  2,406,900  4,938   487.4  65,295 27,169 __333
Gelderland  1,967,600  4,995   393.9  45,043 22,942 _3333
Netherlands 16,254,800 41,525   391.4 440,167 27,132 -3333
Overijssel  1,105,800  3,337   331.4  25,854 23,441 3
Flevoland   356,400  1,426   249.9   6,915 19,439 3
Groningen   575,900  2,344   245.7  18,496 32,245 3
Zeeland   378,300  1,792   211.1   9,354 24,706 3
Friesland   642,500  3,361   191.2  13,989 21,830 3
Drenthe   482,300  2,652   181.9  10,323 21,427  ,181.9
demo date without leading nbsp codes:
0 01-01-2006 0
3k7 abcdefghijk 15,200
pqr abcde 7,500,000
-5 31-12-1999 15 200
g1q4 30-12-1999 7 500 000
2 01-01-2000
3 30q12s99a
4 30q12s9 abc
4 30q12s 9abc

Other 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 · Printing
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 ·  · 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: