Help:Sorting: Difference between revisions
No edit summary |
No edit summary |
||
Line 20: | Line 20: | ||
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. |
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 with comma or space separators, alphabetic sorting can be made to correspond with numeric sorting by leading [[w:Non-breaking space|" " codes]] which render as blank spaces (or with leading zeros). It seems that this cannot be done with the variable [[Help:Magic_words#Formatting|padleft]], because &, the first character of the code, is used for padding, even if the code is put in a template. It would be useful in cases 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. Alternatives are programming an alternative padleft in a template, or using a visible padding character; however it has to be one which in ASCII is before 0, hence one of |
If a column consist of non-negative numbers with comma or space separators, alphabetic sorting can be made to correspond with numeric sorting by leading [[w:Non-breaking space|" " codes]] which render as blank spaces (or with leading zeros). It seems that this cannot be done with the variable [[Help:Magic_words#Formatting|padleft]], because &, the first character of the code, is used for padding, even if the code is put in a template. It would be useful in cases 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. Alternatives are programming an alternative padleft in a template, or using a visible padding character; however it 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 confusing, hence not one of |
||
*$%-. |
*$%-. |
||
Line 35: | Line 35: | ||
*{{padleft:{{#expr:12*13}}|6|,}} |
*{{padleft:{{#expr:12*13}}|6|,}} |
||
*{{padleft:{{#expr:12*13}}|6|/}} |
*{{padleft:{{#expr:12*13}}|6|/}} |
||
*{{padleft:{{#expr:12*13}}|6|0}} |
|||
Not working: |
Not working: |
Revision as of 17:31, 1 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.
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:
- negative numbers (!)
- numbers with comma or space separators
- numbers in scientific notation
- order: numeric according to the first number in the string (parsefloat is applied); if there is no number 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
- criterion: the first element consists of just digits, or digits and a decimal point; not recognized as numeric are:
- date (see also below)
- criterion: the first element is of the form DD-MM-YYYY or DD-MM-YY
- 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
- 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 with comma or space separators, alphabetic sorting can be made to correspond with numeric sorting by leading " " codes which render as blank spaces (or with leading zeros). It seems that this cannot be done with the variable padleft, because &, the first character of the code, is used for padding, even if the code is put in a template. It would be useful in cases 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. Alternatives are programming an alternative padleft in a template, or using a visible padding character; however it 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
If at any time a number without comma or space separators (typically less than 1000) is at the top, the sorting mode is numeric, not sorting properly, instead of alphabetical, even with leading " " codes. In the case of comma separators, a workaround is to put always at least one comma.
Links and other wiki-markup are not possible in headers.
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 " " 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:
- w:List of countries by GDP estimates for 2006 (nominal)
- w:List of countries by GDP (PPP), 2006
- w:List of longest reigning current monarchs - date columns are sortable for people with "no preference" for date formatting, and for people with preference [[YYYY-MM-DD]].
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