Changes

3,288 bytes removed ,  11:14, 6 June 2007
Line 1: Line 1: −
{{H:h}}
+
[[Help:Table|Tables]] can be made sortable via [[client-side JavaScript|client-side JavaScript]] with <code>class="sortable"</code>. Sortable tables are identified by the 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.
[[Help:Table|Tables]] can be made {{mlw|sorting||sortable}} via [[w:client-side JavaScript|client-side JavaScript]] with <code>class="sortable"</code>. This works in MediaWiki 1.9, which is installed in all Wikimedia projects. Sortable tables are identified by the 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.
  −
 
  −
Note that all of the below is subject to change due to improvements in the script.
  −
 
  −
==Javascript==
  −
The JavaScript code [http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/skins/common/wikibits.js?view=log wikibits.js] has on each site a copy at
  −
<nowiki>{{SERVER}}/skins-1.5/common/wikibits.js</nowiki>, on this site {{SERVER}}/skins-1.5/common/wikibits.js. In addition a site may have a page [[MediaWiki:Common.js]] which adds and overrides some code. The description below applies to the version on Meta, {{mlm|MediaWiki:Common.js}}. The sorting code in it can be copied to other sites (by sysops of these other sites).
      
==Sort modes==  
 
==Sort modes==  
The sort modes (the [[w:data type|data type]]s, which, in addition to the choice "ascending" or "descending", determine the sorting order) are as follows; in the given order (as soon as there is a match, subsequent criteria are not applicable, e.g., 24-12-2007 is a date, not a number. For the criteria, tags (e.g. span, sup, sub) are ignored.
+
The sort modes (the [[data type|data type]]s, which, in addition to the choice "ascending" or "descending", determine the sorting order) are as follows; in the given order (as soon as there is a match, subsequent criteria are not applicable, e.g., 24-12-2007 is a date, not a number. For the criteria, tags (e.g. span, sup, sub) are ignored.
 
*date (see also below)
 
*date (see also below)
 
**criterion: the first non-blank element is of the form "dd-dd-dddd", "dd-dd-dd", or "dd aaa dddd"; or, in the last case, text follows ending with "sm=d" (without the quotes; it stands for "sort mode = date"). (In  hidden form this can conveniently be done with {{tim|smd}}.)
 
**criterion: the first non-blank element is of the form "dd-dd-dddd", "dd-dd-dd", or "dd aaa dddd"; or, in the last case, text follows ending with "sm=d" (without the quotes; it stands for "sort mode = date"). (In  hidden form this can conveniently be done with {{tim|smd}}.)
Line 17: Line 10:  
**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
 
**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
 
*numeric
 
*numeric
**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, possibly followed by "×10" and a string consisting of "+",  "-", digits (the latter is for the purpose of using hidden {{mlw|Scientific_notation|E_notation|e-notation}} followed by visible superscript notation of {{mlw|scientific notation||scientific notation}}); or the element ends with "sm=n" (without the quotes; it stands for "sort mode = numeric"). (In  hidden form this can conveniently be done with {{tim|smn}}.)
+
**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, possibly followed by "×10" and a string consisting of "+",  "-", digits (the latter is for the purpose of using hidden {{ml|Scientific_notation|E_notation|e-notation}} followed by visible superscript notation of {{ml|scientific notation||scientific notation}}); or the element ends with "sm=n" (without the quotes; it stands for "sort mode = numeric"). (In  hidden form this can conveniently be done with {{tim|smn}}.)
**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 ([http://www.devguru.com/technologies/ecmascript/QuickRef/parsefloat.html parseFloat] is applied) after removing the commas, if any; if it does not (parseFloat returns [[w:NaN|NaN]]), the element is positioned like 0
+
**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 ([http://www.devguru.com/technologies/ecmascript/QuickRef/parsefloat.html parseFloat] is applied) after removing the commas, if any; if it does not (parseFloat returns [[NaN|NaN]]), the element is positioned like 0
 
:::''proposed internationalisation: in German etc., treat comma as a decimal point''
 
:::''proposed internationalisation: in German etc., treat comma as a decimal point''
 
*string
 
*string
Line 27: Line 20:     
==Examples==
 
==Examples==
:''Remember that the results depend on fixes and enhancements in [[MediaWiki:Common.js]]; the description  applies to the version on Meta. Copies of this help page on other sites are useful for comparison. A sysop can copy the JavaScript-code from Meta.''
  −
   
Text after a number (e.g. a footnote) does not affect the sorting order, if the sorting ''mode'' is numeric. However, if the number at the top has text after it, this makes the sorting mode alphabetic, unless it ends with a (typically invisible) "sm=n".  
 
Text after a number (e.g. a footnote) does not affect the sorting order, if the sorting ''mode'' is numeric. However, if the number at the top has text after it, this makes the sorting mode alphabetic, unless it ends with a (typically invisible) "sm=n".  
   Line 389: Line 380:  
The fourth example shows again that "ca. 12" sorts at 0, as opposed to 12 with some text after it, which sorts at 12. In the latter case an invisible "sm=n" needs to be put at the end, in case such an element arrives at the top of a column, which would otherwise cause alphabetic sorting mode.
 
The fourth example shows again that "ca. 12" sorts at 0, as opposed to 12 with some text after it, which sorts at 12. In the latter case an invisible "sm=n" needs to be put at the end, in case such an element arrives at the top of a column, which would otherwise cause alphabetic sorting mode.
   −
Sortable version of {{mlw|Table:Climate in Middle East cities}}, using smn to specify numeric sort mode:
+
Sortable version of {{ml|Table:Climate in Middle East cities}}, using smn to specify numeric sort mode:
 
{| class="wikitable sortable" style="margin-left:.6em;float:right;"
 
{| class="wikitable sortable" style="margin-left:.6em;float:right;"
 
|-
 
|-
Line 576: Line 567:  
|Second
 
|Second
 
|}
 
|}
  −
See also [[w:Talk:List_of_U.S._states_by_population#Sortable_Table]].
      
===Padding with zeros===
 
===Padding with zeros===
Line 838: Line 827:     
If at some point (i.e., after possible previous sorting) the form <code><nowiki>[[YYYY]]</nowiki></code> is at the top with a non-negative year, sorting would be numerical; in this case, after toggling between ascending and descending there would be 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 only that number). Also, years BC would not be sorted properly. Therefore, alphabetic sorting has to be enforced. This can be done by putting a non-displayed character after the year, separated by a space.
 
If at some point (i.e., after possible previous sorting) the form <code><nowiki>[[YYYY]]</nowiki></code> is at the top with a non-negative year, sorting would be numerical; in this case, after toggling between ascending and descending there would be 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 only that number). Also, years BC would not be sorted properly. Therefore, alphabetic sorting has to be enforced. This can be done by putting a non-displayed character after the year, separated by a space.
  −
See also:
  −
*{{tiw|dts}}
  −
*[[bugzilla:8226]].
      
==Secondary sortkey==
 
==Secondary sortkey==
If a column contains a value multiple times then sorting the column preserves the order of the rows within each subset that has the same value in that column ({{mlw|Sorting_algorithm|Stability|stable sorting}}). Thus sorting based on a primary, secondary, tertiary, etc. sortkey can be done by sorting the least-significant sortkey first, etc.
+
If a column contains a value multiple times then sorting the column preserves the order of the rows within each subset that has the same value in that column ({{ml|Sorting_algorithm|Stability|stable sorting}}). Thus sorting based on a primary, secondary, tertiary, etc. sortkey can be done by sorting the least-significant sortkey first, etc.
    
'''First click on column Alphabet and then on Numbers, you'll see that the ordering is on Numbers (1), Alphabet (2).'''
 
'''First click on column Alphabet and then on Numbers, you'll see that the ordering is on Numbers (1), Alphabet (2).'''
Line 1,030: Line 1,015:     
The two-character entries such as A1 demonstrate that A and a are at the same position.
 
The two-character entries such as A1 demonstrate that A and a are at the same position.
  −
==See also==
  −
*{{ml|Help:Collapsing|Sortable_collapsible_table|Sortable collapsible table}}
  −
*{{tiw|dts}} - sorting a table by a date column, while following [[w:Wikipedia:Manual_of_Style_%28dates_and_numbers%29#Dates_containing_a_month_and_a_day]] for display
  −
*{{tim|sd}} - shows buggy date sorting when some or all dates are linked
  −
*[[Help:Table]]
  −
*[http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/skins/common/wikibits.js wikibits.js] - contains, among other things, the sorting code
  −
**for revision history see also [http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/skins/common/sorttable.js?view=log&pathrev=19698 sorttable.js]
  −
**{{mlm|MediaWiki:Common.js}} page overriding function ts_parseFloat(num) and function ts_resortTable(lnk) on Meta with improved versions
  −
**{{mlw|MediaWiki:Common.js}} page overriding function ts_parseFloat(num)
  −
**[[sv:MediaWiki:Common.js]] page overriding function ts_parseFloat(num); adaptation to decimal comma
  −
*[[bugzilla:2001]] - resolved feature request
  −
*[[bugzilla:8063]] - request to enable sorting of numbers with a point as thousands separator
  −
*[[bugzilla:8115]]
  −
*http://www.kryogenix.org/code/browser/sorttable/ - explanation of the original version of the code by Stuart Langridge; an improvement in the MediaWiki version is that tables no longer need to have an id.
  −
*http://blog.webkist.com/archives/000043.html
  −
*http://www.joostdevalk.nl/code/sortable-table/
  −
*[[w:Wikipedia:Wikipedia Signpost/2007-01-02/Technology report]]
  −
*{{tiw|sort}}
  −
*[[mw:Extension:Sort2]]
  −
*[[mw:Extension:Sort]]
  −
*[[w:User:TimR/Tables]]
  −
  −
Examples elsewhere:
  −
  −
*[[w:Ranked list of Dutch provinces]]
  −
*[[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 <nowiki>[[YYYY-MM-DD]]</nowiki>.
  −
*[[w:User:Smurrayinchester/Template|Pokémon table]] - prefixes numbers with "#" to have them before "-".
  −
*[[w:Nuclear_power_by_country|Nuclear power by country]] (Featured List)
  −
*[[w:Desert Island Discs: castaways' choices]]
  −
  −
  −
  −
{{h:f|enname=Sorting}}
 
2,646

edits