Content deleted Content added
HouseBlaster (talk | contribs) →Forcing a column to have a particular data type: e.g. is for examples, i.e. is for definitions. ISO 8601 *is* YYYY-MM-DD format, so use i.e. Also remove "Proposed internationalisation", because a help page is for what people can do, not a wishlist of what they should be able to do. |
Timeshifter (talk | contribs) note about mobile app problem |
||
(46 intermediate revisions by 12 users not shown) | |||
Line 1:
{{short description|Help with sorting of rows in tables}}
{{About||other uses|Help:Sorting}}
{{Table help}}
{{Wikipedia how to|WP:TABLESORT|WP:SORT}}
{{Wiki markup}}
{{TOC limit|3}}
{{hatnote|'''Note:''' Tables are sortable in mobile browsers, but not in the [[wikipedia mobile app|mobile app]]. See [[phab:T181452]].}}
== Creating sortable tables ==
[[File:Wikipedia VisualEditor Table Properties.png|thumb|The properties panel of a table in VisualEditor allows you to mark a table as sortable.]]
Tables can be made sortable via [[client-side JavaScript]] by adding <code>class="wikitable sortable"</code> to their top line. These tables need to be properly formatted, with the correct number of cells. Additionally, you need to make sure that the headers of your column are properly indicated in the [[Help:Wiki markup|wikitext]]. For this, the <code>!</code> character is used in the table syntax.
If you are using the [[Wikipedia:VisualEditor|Visual Editor]], you can open the properties dialog of a table and select the sortable option.
=== Simple example ===
This is the wikitext of the table [[Help:Sortable tables#Example|shown]] in the first section and shows the typical way to enable table sorting:
<syntaxhighlight lang="wikitext">
{| class="wikitable sortable"
|-
! name
! data
! more data
|-
| cats
| 273
| 53
|-
| dogs
| 65
| 8,492
|-
| mice
| 1,649
| 548
|}
</syntaxhighlight>
The <code>!</code> indicates cells that are header cells. In order for a table to be sortable, the first row(s) of a table need to be entirely made up out of these header cells. You can learn more about the basic table syntax by taking the [[Help:Table/Introduction to tables|Introduction to tables]] for source editing.
=== Initial sort order of rows ===
When users are first presented with a table, the rows will always appear in the same order as in the wikitext. If you want a table to appear sorted by a certain column, you must sort the wikitext itself in that order. This is usually done for the first column. The VisualEditor makes it easy to move individual table columns and rows around. For info about that, and also about putting a table in initial alphabetical order see [[#initial alphabetical order|§ Initial alphabetical order]].
== Using sortable tables ==
When browsing Wikipedia you may encounter [[Help:Table|tables]] that have been made ''[[Help:Table#Sorting|sortable]]''. A sortable table is
Sortable tables are particularly useful for organizing and navigating large sets of data. For example, you might have a table displaying names, dates, or numerical data. By making the table sortable, you allow readers to click on the column header to sort by, for example, alphabetical order (A–Z or Z–A) for names, chronological order for dates, or numerical order for numbers (low to high or high to low).
The actual sorting process will happen on
=== Example ===
This is an example of a small
<div style="display:inline-table; border: 2px solid lime; padding: 0.5em;">
Line 112 ⟶ 150:
</syntaxhighlight></div>
Using two or more header rows, the sort arrows are placed on the bottom or lowest header row by default. They can be placed a maximum of one row higher than the lowest header row by setting <code>class="sorttop"</code> at the top of the bottom header row. For example:
<div style="display:inline-table; border: 2px solid lime; padding: 0.5em;">
Line 188 ⟶ 226:
</div>
=== Tables with
{{Anchor|Tables with complex data rows}}
{{Further|Help:Tables#Colspan and rowspan|Wikipedia:Manual of Style/Accessibility/Data tables tutorial#Complex tables}}
:
It is possible to create tables with cells that stretch over two or more columns or rows (also known as merged cells). For columns, one uses <code>|colspan=<var>n</var> | <var>content</var></code>, whereas for rows, one uses <code>|rowspan=<var>m</var> | <var>content</var></code>. In the table code, one must ''leave out the cells that are covered by such a span''. The resulting column- and row-counting must fit. Tables can have cells spanning multiple rows, using <code>|rowspan=''n''</code>.
The number of rows must be indicated with each use of rowspan. Before any sorting can be done, the rowspan setup must be correct. The wikitext must be correct. An incorrect rowspan organization can break sorting, cause weird table formatting, move data to the wrong column, etc.
Line 299 ⟶ 339:
* [http://wikitable.eu5.org wikitable.eu5.org]
It makes it easy to edit the text and links in individual cells of a table. It is especially easy when there are no rowspans in the body of a table. See the [[Help:Sortable tables#Previous section|previous section]]. Without rowspans it is easier to change the underlying framework of a table, and move stuff around. Once the wikitext framework is simpler, the online table editor is simpler too, because you
=== Secondary key and multi-key sorting ===
Another
There can be a problem with sorting if the [[User:Endo999/GoogleTrans|Google translation gadget]] (see [[User talk:Endo999/GoogleTrans#Interference with secondary sorting in tables|discussion]]) is enabled in {{Myprefs|Gadgets|uncheck=GoogleTrans}}. It may seem like shift-click secondary sorting is not working because of the delay due to the translation popup for the name in the column head. The gadget can be turned on and off quickly, once enabled in preferences, from the "Tools" menu at the upper right of any page. There are many browser translation addons (such as Simple Translate for [https://addons.mozilla.org/en-US/firefox/addon/simple-translate Firefox], [https://microsoftedge.microsoft.com/addons/detail/simple-translate/cllnohpbfenopiakdcjmjcbaeapmkcdl Chrome], and [https://chrome.google.com/webstore/detail/simple-translate/ibplnjkanclpjokhdolnendpplpjiace Edge]) that work as well or better than the gadget.
{|class="wikitable sortable"
!Numbers!!Text!!More text
Line 339 ⟶ 378:
|}
==
=== Vertical headers ===
Line 398 ⟶ 437:
</syntaxhighlight></div>
<div style="display:inline-table; border: 2px solid lime; padding: 0.5em;">
Line 460 ⟶ 499:
== Sort under template ==
{{Tracked|T35249}}
{{t|sort under}} can be used to make the sorting arrows appear underneath the header text, which may be desirable to narrow the width of a column.
{{sort under}}
Line 490 ⟶ 523:
| 548
|}
== Restrictions and exclusions ==
Tables can only
=== Making selected columns unsortable ===
Line 703 ⟶ 702:
=== Excluding top rows from sorting ===
Table below is an adapted excerpt from [[List of U.S. states and territories by poverty rate]]. '''Sorttop''' is used on the US data row. And the US row is excluded from having a static row number. Note that the "Area" flag links can't be made bold the normal way. But the whole row can be made bold like this:
<syntaxhighlight lang=wikitext highlight=1>
|- class="sorttop static-row-numbers-norank" style="font-weight:bold;"
| {{flagg|uspeft|pref=Economy of|United States}}
| 12.5 || 0.1 || 40,763,043 || 277,214
</syntaxhighlight>
{{mw-datatable}}{{table alignment}}{{static row numbers}}{{sticky header}}
{| class="wikitable sortable sticky-header mw-datatable col1left static-row-numbers" style=text-align:right;
|+ Number and percentage of people in poverty by state, D.C., and Puerto Rico. Plus margin of error. 2023.
! Area !! Percent !! Error (±) !! Number !! Error (±)
|- class="sorttop static-row-numbers-norank" style="font-weight:bold;"
| {{flagg|uspeft|pref=Economy of|United States}}
| 12.5 || 0.1 || 40,763,043 || 277,214
|-
| {{flagg|uspeft|pref=Economy of|Puerto Rico}}
| 39.6 || 0.8 || 1,257,607 || 26,856
|-
| {{flagg|uspeft|pref=Economy of|Louisiana}}
| 18.9 || 0.6 || 840,146 || 27,162
|-
| {{flagg|uspeft|pref=Economy of|Mississippi}}
| 18.0 || 0.8 || 512,184 || 21,949
|}
Note that sorttop can't be used with multi-header rows in tables using {{tl|sticky table start}}. Single-row headers are not a problem.
== Configuring the sorting ==
By default, the system tries to guess the data type in each column. It does this by looking at the first five rows and evaluating their contents. This process works most of the time but can also easily get confused if you have inconsistent values or additional specifiers that the system doesn't know about. To avoid this ambiguity you can force a particular data type or override the value of a cell. For numeric values, consider using template {{Tl|Val}}, see examples at {{Section link|Template:Val|Sorting}}.
== Forcing a column to have a particular data type ==
{{See|
The <code>data-sort-type="..."</code> attribute can be added inside the header of a column to ensure that the cells underneath are ''all'' treated as a specified type of data.
The following (case-insensitive) values are valid for <code>data-sort-type</code>:
Line 721 ⟶ 748:
* <code>date</code> for language specific standard date format
* <code>isoDate</code> for dates in [[ISO 8601]] format (i.e. YYYY-MM-DD)
* <code>usLongDate</code> for dates in the US format
* <code>time</code>
Line 816 ⟶ 843:
Without <code>data-sort-type=text</code> in the header, the tablesorter gets confused by the numeric titles in the first few rows into treating the entire column as numeric. This results in it wrongly sorting the non-numeric titles as zero regardless of the alphabetical ordering of their text.
Note that if a column without declared sort-type contains only numeric values within the first top
=== Default data type of a column ===
Line 829 ⟶ 856:
:* criterion: the first non-blank element starts with $, £, €, or ¥
:* order: numeric, ignoring these symbols and all ordinary letters and commas, but not spaces; note that scientific notation cannot be used, as e and E are removed
:* Note: Force this if column is currency. Otherwise, no sorting at all may occur.
; number
:* criterion: the first non-blank element consists of just digits, points, commas, spaces, "+", "-", possibly followed by "e" or "E" and a string consisting of "+", "-", digits
Line 840 ⟶ 868:
The sort mode is determined by the first 5 non-blank rows below the header after loading the page. This can also change after deleting a row, or adding a column. Therefore, it is wise to make sure that every element matches the criterion for the required data type. Using a [[Help:Table#Row template|row template]] this can be done very conveniently.
The method of making sure the sort mode of each column is as desired, is specify a <code>data-sort-type</code>, see [[#Forcing a column to have a particular data type|
== Specifying a sort key for a cell ==
Line 929 ⟶ 957:
See also [[mw:Help:Sorting#Specifying a sort key]].
'''If you have a list where all the entries start with quotes ("),''' and you want to set a sort key for one of the entries, then you will need to use the
<code style="
<code style="
=== Keeping some rows together ===
Line 1,247 ⟶ 1,275:
To work '''<code>data-sort-type=number</code>''' needs to be in the '''header cell''' that '''contains the sorting icon.''' In tables with multi-row headers, the sorting icon will be in the lowest header cells.
When '''<code>data-sort-type=number</code>''' is '''not in the header''' the following is true:
*References <code><nowiki><ref>...</ref></nowiki></code> after a number in any cell (including the first five cells) no longer break numerical sorting. *The same is true for the percent symbol (%) after a number.
*Text breaks numerical sorting if it is before or after a number in one of the first 5 cells in a column.
*Asterisks (*) in the first 5 cells break numerical sorting.
*A colon by itself (to signify no data, for example) in one of the first 5 cells in a column breaks numerical sorting.
*A dash, of any kind, ''in a blank cell'' in one of the first 5 cells in a column breaks default numerical sorting of a column.
*A dash ''in front of a number'' does not break numerical sorting.
Dashes are allowed anywhere in cells if <code>data-sort-type=number</code> is used in the column header.
Leading zeroes are not necessary for numerical sorting of a column. If it seems that way, then that means the column is being sorted alphabetically. Look in the first 5 cells for anything other than numbers, and correct those cells according to these rules. Better yet, add <code>data-sort-type=number</code> to the column header. Later editing by other editors will not break numerical sorting.
The {{tl|N/A}} template in the first five cells of a column is inconsistent in its effect on automatic datatype detection. It is always better to specify a <code>data-sort-type</code> in the column header.
Line 1,470 ⟶ 1,499:
== Date sorting problems ==
The '''{{Tl|Date table sorting}}''' or '''{{tl|dts}}''' template will work with any combination of years, months, days
=== Month names ===
Line 1,633 ⟶ 1,662:
|}
===
This shows that any full date works ''without'' '''data-sort-type=date''' in the table. Month-day-year or day-month-year. If a number for a day is missing, date sorting is broken. Abbreviated months work too. The year must be on the end, and a number can not be used for the month. Either will break date sorting.
<div style="display:inline-table; vertical-align:top;">
{| class="wikitable sortable"
|-
! Full dates
|-
| Dec 5, 1999
Line 1,651 ⟶ 1,679:
|-
| Aug 4, 2004
|-
| 5 Dec 1999
|-
|
|-
|
|-
|
|}
</div>
<div style="display:inline-table; vertical-align:top;">
{| class="wikitable sortable"
|-
! Full dates
|-
|
|-
|
|-
| May 14, 2004
|-
|
|-
| 5 December 1999
|-
|
|-
|
|-
|
|}
</div>
'''data-sort-type=date''' in the header for the date column allows date sorting to work for all entries not missing a day number. So it is good to add it in case editors forget day numbers for some entries. That can be fixed later. The rest of the dates will sort correctly by date. Test date sorting below. '''Some entries below are missing the day number.''' They end up at the top or bottom of the ''data-sort-type=date'' list upon sorting. The list below without ''data-sort-type=date'' does not sort by date.
The 2 tables below are identical except for the addition of '''data-sort-type=date''' to the second table.
<syntaxhighlight lang=wikitext>
{| class="wikitable sortable"
|-
! data-sort-type=date | Full dates.<br><code>data-sort-type=date</code>
</syntaxhighlight>
<div style="display:inline-table; vertical-align:top;">
{| class="wikitable sortable"
|-
! Full dates.<br>A mix of formats
|-
| Dec 5, 1999
|-
| Jan 7, 1999
|-
| May 2004
|-
| Aug 4, 2004
|-
| 5 Dec 1999
Line 1,715 ⟶ 1,742:
|-
| 4 Aug 2004
|-
| December 1999
|-
| January 7, 1999
|-
| May 14, 2004
|-
| August 4, 2004
|-
| 5 December 1999
|-
|
|-
| 14 May 2004
Line 1,729 ⟶ 1,759:
| 4 August 2004
|}
</div>
<div style="display:inline-table; vertical-align:top;">
{| class="wikitable sortable"
|-
! data-sort-type=date |
|-
| Dec 5, 1999
|-
| Jan 7, 1999
|-
| May 2004
|-
| Aug 4, 2004
|-
| 5 Dec 1999
Line 1,745 ⟶ 1,781:
|-
| 4 Aug 2004
|-
| December 1999
|-
| January 7, 1999
|-
| May 14, 2004
|-
| August 4, 2004
|-
| 5 December 1999
|-
|
|-
| 14 May 2004
Line 1,759 ⟶ 1,798:
| 4 August 2004
|}
</div>
=== Year, month, day. Using words for months ===
Line 2,011 ⟶ 2,050:
</div>
== Date table sorting template
{{anchor|Date table sorting template}}
The simplest way to format sortable dates in a table is to use the '''{{tl|Date table sorting}}''' template. A redirect: '''{{tl|dts}}'''
It can be used with many date formats mixed together
<div style="display:inline-table; vertical-align:top;">
Line 2,440 ⟶ 2,477:
=== Putting a table in initial alphabetical order ===
<span id="initial alphabetical order"></span>
{{See|Help:
{{See also|Phab:T240114}}
There are fast and slow ways to do this. Unfortunately, the [[WP:VE|Visual Editor]] does not have a fast way to quickly alphabetize a table. You can manually move rows around one at a time. Click on a cell in a row. Then click on the arrow that shows up at the left of the row. Then click on "move above" or "move below" as needed. If this is buggy or is not working, the rows can be moved around in the wikitext by cutting and pasting rows in the wikitext.
'''A fast way''' is to launch free '''[[w:LibreOffice Calc|LibreOffice Calc]],''' or [[List of spreadsheet software|another spreadsheet program]]. To see how go to '''[[Help:
'''There is another way''' to alphabetize a table, and it keeps all the styling and flag links that a spreadsheet may remove. One can use '''NoteTab Light''' (freeware version of NoteTab). But for this to work, all the wikitext for a row must be on one line. That means the cells in that row are separated by double bars {{big|'''<code><nowiki>||</nowiki></code>'''}}.
|