This page gives some examples of my extensions to sortable tables. Currently, sorting is done on the content of table cells, which is a good start, but not very flexible. The current guidance illustrates a workaround by using non-displayed text:
- The most versatile is alphabetic sorting using a sortkey which due to CSS is not displayed:
- <span style="display:none">...</span>
-
- (however, this more cumbersome method is less often needed after the described fixes are applied)
but this is a little clumsy to use.
I have adapted the table sorting code slightly to allow a sorting key for a cell to be defined anywhere in that cell. To see this in action, you need to edit your monobook.js file to include the following line:
importScript('User:TimR/sorttable.js');
Without it, the following tables will still be sortable, but will sort according to the default rules. With this line added to your configuration, you will be able to see these new sorting mechanisms.
Specifying simple keywords
editProvided that the word or phrase which you wish to use to sort on is already in a cell, you can simply mark it with the {{sortkey}} template. It will still get displayed just as it would without the template, but its value will be used to sort the table. For example, it can be used to mark a person's surname so that the column will sort on that:
{| class="wikitable sortable" !Astronomer Royal!!From!!To |- |Rev'd John {{sortkey|Flamsteed}}||1675||1719 |- |Professor Edmond {{sortkey|Halley}}||1720||1742 |- |Dr James {{sortkey|Bradley}}||1742||1762 ... |Martin {{sortkey|Rees}}, Baron Rees of Ludlow||1995|| |- |}
Of course, the table still looks the same, but it sorts properly. Here is the example. (Remember that it will not sort properly unless you have edited your monobook.js file.) Try clicking on the icon at the top of the first column to see the sort by surname.
Astronomer Royal | From | To |
---|---|---|
Rev'd John | 1675 | 1719 |
Professor Edmond | 1720 | 1742 |
Dr James | 1742 | 1762 |
Nathaniel | 1762 | 1764 |
Rev'd Nevil | 1765 | 1811 |
John | 1811 | 1835 |
Sir George Biddell | 1835 | 1881 |
Sir William | 1881 | 1910 |
Sir Frank | 1910 | 1933 |
Sir Harold Spencer | 1933 | 1955 |
Professor Sir Richard | 1956 | 1971 |
Professor Sir Martin | 1972 | 1982 |
Professor Sir Francis | 1982 | 1990 |
Professor Sir Arnold | 1991 | 1995 |
Martin , Baron Rees of Ludlow | 1995 |
Specifying more complex content
editIn many cases, you want more than just plain text in a table cell, such as links. You may also want to specify a sort key for this content. This is where a sort key and other markup might come into conflict.
If you simply want to specify the text of a link as the sort key, you simply make the link inside the template, as shown here:
{| class="wikitable sortable" !Date!!Day!!Present |- |{{hiddenkey|20070105}}[[5 January]] [[2007]] |12||twelve {{sortkey|[[drummers]]}} drumming |- |[[4 January]] [[2007]]{{hiddenkey|20070104}} |11||eleven {{sortkey|[[pipers]]}} piping ... |{{hiddenkey|20061225}}[[25 December]] [[2006]] |1||a {{sortkey|[[partridge]]}} in a pear tree |- |}
Note that in the case of piped links, the displayed text acts as the sort key, not the article name.
With this table, we also want to be able to sort on the first column by date. In this case we need to specify a sort key which can be used to sort the rows in the right order, but doesn't get displayed. We do that with the {{hiddenkey}} template. The usage is just the same as the {{sortkey}} template: it can come anywhere in the cell and you simply specify the sort key, but the key itself is invisible. I have demonstrated this by putting it in different places within the cell in various rows in the table:
{| class="wikitable sortable" !Date!!Day!!Present |- |{{hiddenkey|20070105}}[[5 January]] [[2007]] |12||twelve {{sortkey|[[drummers]]}} drumming |- |[[4 January]] [[2007]]{{hiddenkey|20070104}} |11||eleven {{sortkey|[[pipers]]}} piping ... |{{hiddenkey|20061225}}[[25 December]] [[2006]] |1||a {{sortkey|[[partridge]]}} in a pear tree |- |}
So here is the final product. Try playing with the columns to see how it sorts.
Date | Day | Present |
---|---|---|
5 January 2007 | 12 | twelve drumming |
4 January 2007 | 11 | eleven piping |
3 January 2007 | 10 | ten a-leaping |
2 January 2007 | 9 | nine dancing |
1 January 2007 | 8 | eight a-milking |
31 December 2006 | 7 | seven a-swimming |
30 December 2006 | 6 | six a-laying |
29 December 2006 | 5 | five |
28 December 2006 | 4 | four |
27 December 2006 | 3 | three |
26 December 2006 | 2 | two |
25 December 2006 | 1 | a in a pear tree |