Help:Sorting
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)
- 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).
A table element consisting of just digits, or digits and a decimal point, is considered numeric. A negative number and a number in scientific notation are not recognized as number in determining the sorting mode, but if numeric sorting is used, these numbers are properly numerically sorted. Numbers with spaces or commas are treated as strings. If all numbers in a column are non-negative, alphabetic sorting can be made to correspond with numeric sorting by leading zeros, or " " codes which render as blank spaces, typically giving a visually more attractive result.
Links and other wiki-markup do not seem 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
With leading " " codes:
Province | Population | Area (km²) | Density | GDP(PPS in mil. € 2003) | GDP per cap. (in € 2003) |
---|---|---|---|---|---|
South Holland | 3,453,000 | 2,860 | 1,207.3 | 95,868 | 27,825 |
a | 2,583,900 | 2,660 | 971.4 | 65,295 | 27,169 |
Utrecht | 1,159,200 | 1,356 | 854.9 | 38,355 | 33,148 |
Limburg | 1,143,000 | 2,167 | 527.5 | 28,038 | 24,585 |
North Brabant | 2,406,900 | 4,938 | 487.4 | 65,295 | 27,169 |
Gelderland | 1,967,600 | 4,995 | 393.9 | 45,043 | 22,942 |
Netherlands | 16,254,800 | 41,525 | 391.4 | 440,167 | 27,132 |
Overijssel | 1,105,800 | 3,337 | 331.4 | 25,854 | 23,441 |
Flevoland | 356,400 | 1,426 | 249.9 | 6,915 | 19,439 |
Groningen | 575,900 | 2,344 | 245.7 | 18,496 | 32,245 |
Zeeland | 378,300 | 1,792 | 211.1 | 9,354 | 24,706 |
Friesland | 642,500 | 3,361 | 191.2 | 13,989 | 21,830 |
Drenthe | 482,300 | 2,652 | 181.9 | 10,323 | 21,427 |
demo | date | without leading nbsp codes: |
---|---|---|
0 | 01-01-2006 | 0 |
3k7 | abcdefghijk | 15,200 |
pqr | abcde | 7,500,000 |
-5 | 31-12-1999 | |
g1q4 | 30-12-1999 | |
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