Help talk:Sorting

From Wikipedia, the free encyclopedia

Contents

[edit] Possible improvement for Template:Dts

I saw that {{Dts}} was copied from m:Template:Dts. Help:Sorting is pretty cool stuff. But since I'm a lazy programmer and wikieditor, I'm always looking for a way to cut corners. Check out {{User:MrDolomite/Sandbox/Dts}} and let me know what you all think. — MrDolomite | Talk 18:04, 11 January 2007 (UTC)

It's caused a couple problems already, and we have the opportunity to fix them before it gets used everywhere and there's lots of fixin' to do. Check out Template Talk:Dts for more. —Wikibarista 18:04, 21 March 2007 (UTC)

[edit] Sort key

Is it possible (or would it be possible) for cells to have a sort key different from that which is displayed? I want to make List of Liverpool F.C. players sortable, but for it to be useful I really need:

  • To sort the names by surname, firstname
  • To sort the flag icons by nation name
  • To sort the positions in the order of (roughly) GK, Defender, Midfielder, Forward.

--ArtVandelay13 13:19, 11 February 2007 (UTC)

No, sorry, that is not possible right now. The obvious solution to issue #1 is to make the displayed text itself in Last, First format. The obvious solution to issue #2 is to convey the information in words rather than with an image. Christopher Parham (talk) 23:47, 12 February 2007 (UTC)
Perhaps the best solution for sorting by images (e.g national flags) is to have images be sortable by alt text. Pimlottc 01:46, 13 February 2007 (UTC)
Please check out the discussion underway at Template talk:Sortname. You can use {{sortname}} today to solve the first problem, and {{sort}} or {{sortkey}} to solve the other two. Andrwsc 00:24, 5 April 2007 (UTC)

[edit] colspan=2

The script, while ingenious, doesn't work very well with headers spanning over several columns, effectively screwing up List of members of the Riksdag, 2002-2006 by requiring a separate header for the colors (resulting in that column being unnecessarily wide). Solution, anyone? Jobjörn (Talk ° contribs) 21:03, 22 February 2007 (UTC)

[edit] Tables not being sorted

For some reason I'm no longer able to get tables in WP to obey the instruction class="wikitable sortable". Is this a known problem? JavaScript is not disabled on my system.

It must be something to do with my WP Preferences, because the problem disappears when I log out. I don't remember changing anything in the Prefs, however: any suggestions? I've tried bypassing my cache, but that didn't help.

What would I need to do (or have done) to disable the "sortable" property on my computer? -- NigelG (or Ndsg) | Talk 09:41, 13 March 2007 (UTC)

PS What I really meant is: the buttons don't appear at the top of the columns. When I log out they do appear, & the table sorts correctly. -- NigelG (or Ndsg) | Talk 17:54, 13 March 2007 (UTC)
It's OK: I've got to the root of the problem, which was in my monobook.js. Panic over! -- NigelG (or Ndsg) | Talk 18:06, 14 March 2007 (UTC)
I get exactly the same problem. If I'm logged in then "sortable" just seems to be ignored. If I log out or empty my monobook.js then it works. Does anybody have more information on what we need to fix in monobook.js to get sortable tables to work.--MarkS(talk) 07:16, 7 April 2007 (UTC)
Found the problem. I use the Extra Edit Buttons script. This had a function with the name getElementsByClassName which prevented sortable tables from working. I guess, therefore, that the sortable script also has the same function name. I've change extra edit buttons to use a different name. I guess though that other scripts could also include a function with this name so watch out. --MarkS(talk) 21:10, 7 April 2007 (UTC)

[edit] Sortkeys

I started a discussion at Template talk:Sort, since there seem to be a number of unconnected efforts to create sortkeys for sortable wikitables. ~ trialsanderrors 23:21, 2 April 2007 (UTC)

[edit] number sorting

I've read this page and looked at the code in the example table, but I don't quite get what is the best way to get numbers of varying lengths to sort properly (i.e. for 17,000,000 to show up as larger than 900,000). In particular, I would like to fix the attendance numbers at List of sports attendance figures. Thanks for any help. — Reinyday, 00:15, 5 April 2007 (UTC)

Try the {{nts}} template! You would write {{nts|17000000}} to render &0000000017000000.00000017,000,000, and the sort keys would work as expected. Andrwsc 00:22, 5 April 2007 (UTC)
  • Excellent. Thanks! — Reinyday, 19:03, 6 April 2007 (UTC)

[edit] <datagrid>?

Do you plan to ultimately use datagrid?
David Latapie ( | @) — www 19:35, 10 April 2007 (UTC)

[edit] help

I need the correct code for Instructions_per_second anywere can help? -- FidelFair 04:13, 6 May 2007 (UTC)

Processor IPS (sortable don´t work?) Year Source
Pencil and Paper 0.0119 IPS 1892 [1]
IBM System/370 model 158-3 1 MIPS 1972
Intel 8080 640 kIPS at 2 MHz 1974
VAX 11/780 500 kIPS 1977
Motorola 68000 1 MIPS at 8 MHz 1979
Intel 386DX 8.5 MIPS at 25 MHz 1988
Intel 486DX 54 MIPS at 66 MHz 1992
PowerPC 600s (G2) 35 MIPS at 33 MHz 1994
Intel Pentium Pro 541 MIPS at 200 MHz 1996 [2]
ARM 7500FE 35.9 MIPS at 40 MHz 1996
PowerPC G3 525 MIPS at 233 MHz 1997
Zilog eZ80 80 MIPS at 50 MHz 1999 [3]
Intel Pentium III 1354 MIPS at 500 MHz 1999
AMD Athlon 3561 MIPS at 1.2 GHz 2000
AMD Athlon XP 2400+ 5935 MIPS at 2.0 GHz 2002
Pentium 4 Extreme Edition 9726 MIPS at 3.2 GHz 2003
ARM Cortex A8 2000 MIPS at 1.0 GHz 2005 [4]
Xbox360 IBM "Xenon" Triple Core 6400 MIPS at 3.2 GHz 2005
AMD Athlon FX-57 12000 MIPS at 2.8 GHz 2005
AMD Athlon 64 3800+ X2 (Dual Core) 14564 MIPS at 2.0 GHz 2005 [5]
AMD Athlon FX-60 (Dual Core) 18938 MIPS at 2.6 GHz 2006 [6]
Intel Core 2 X6800 27079 MIPS at 2.93 GHz 2006 [7]
IBM Cell one SPE 25600 MIPS (MFLOPS) at 3.2 GHz 2006
IBM Cell one PPE 51200 MIPS (38400 MFLOPS)at 3.2 GHz 2006
IBM Cell BE (1PPE + 8SPEs) 256000 MIPS (MFLOPS) at 3.2 GHz 2006
SONY PS3 (1PPE + 7SPEs) 230400 MIPS (217600 MFLOPS) at 3.2 GHz 2006
Intel Core 2 Extreme QX6700 57063 MIPS at 3.33 GHz 2006 [8]
This seems to work:
Processor IPS Year Source
Pencil and Paper 0.0119 IPS 1892 [9]
IBM System/370 model 158-3 1 MIPS 1972
Intel 8080 640 kIPS at 2 MHz 1974
VAX 11/780 500 kIPS 1977
Motorola 68000 1 MIPS at 8 MHz 1979
Intel 386DX 8.5 MIPS at 25 MHz 1988
Intel 486DX 54 MIPS at 66 MHz 1992
PowerPC 600s (G2) 35 MIPS at 33 MHz 1994
Intel Pentium Pro 541 MIPS at 200 MHz 1996 [10]
ARM 7500FE 35.9 MIPS at 40 MHz 1996
PowerPC G3 525 MIPS at 233 MHz 1997
Zilog eZ80 80 MIPS at 50 MHz 1999 [11]
Intel Pentium III 1354 MIPS at 500 MHz 1999
AMD Athlon 3561 MIPS at 1.2 GHz 2000
AMD Athlon XP 2400+ 5935 MIPS at 2.0 GHz 2002
Pentium 4 Extreme Edition 9726 MIPS at 3.2 GHz 2003
ARM Cortex A8 2000 MIPS at 1.0 GHz 2005 [12]
Xbox360 IBM "Xenon" Triple Core 6400 MIPS at 3.2 GHz 2005
AMD Athlon FX-57 12000 MIPS at 2.8 GHz 2005
AMD Athlon 64 3800+ X2 (Dual Core) 14564 MIPS at 2.0 GHz 2005 [13]
AMD Athlon FX-60 (Dual Core) 18938 MIPS at 2.6 GHz 2006 [14]
Intel Core 2 X6800 27079 MIPS at 2.93 GHz 2006 [15]
IBM Cell one SPE 25600 MIPS (MFLOPS) at 3.2 GHz 2006
IBM Cell one PPE 51200 MIPS (38400 MFLOPS)at 3.2 GHz 2006
IBM Cell BE (1PPE + 8SPEs) 256000 MIPS (MFLOPS) at 3.2 GHz 2006
SONY PS3 (1PPE + 7SPEs) 230400 MIPS (217600 MFLOPS) at 3.2 GHz 2006
Intel Core 2 Extreme QX6700 57063 MIPS at 3.33 GHz 2006 [16]
Enjoy. —EncMstr 19:56, 7 May 2007 (UTC)
thx --FidelFair 09:20, 8 May 2007 (UTC)

[edit] collapsable + sortable?

Is it possible to get a collapsable and sortable table? ∞ΣɛÞ² (τ|c) 12:30, 26 May 2007 (UTC)

Header 1 Header 2 Header 3
A1 B1 C1
A2 B2 C2
Apparently. --Van helsing 09:29, 30 May 2007 (UTC)
Collapsible table
Header 1 Header 2 Header 3
A1 B1 C1
A2 B2 C2
or tables in tables.

[edit] Numeric sorting doesn't work

I've made a sortable wikitable for a "crimes against humanity" table, here, but I couldn't get the estimates columns to sort properly. I know what the problem is in part, which is that because there are references together with the estimates in the first cell, the sort table reads as an alphabetic table instead of a numeric one.

The Help page says you just have to finish your cell with the code "sm=n" to make the sorting in such cases numeric, but it doesn't work. I found another way of making the table work by creating hidden rows at the top and bottom of the table with very high and low numbers respectively, but it's a kludgy fix and doesn't work right under all conditions.

Can someone please show me how to get this "sm=n" method to work? Thanks, Gatoclass 16:45, 15 June 2007 (UTC)

It requires the relevant code in m:MediaWiki:Common.js to be copied to MediaWiki:Common.js.--Patrick 23:04, 17 June 2007 (UTC)
Do you know how to do that? I have no idea. Gatoclass 23:14, 17 June 2007 (UTC)
It is not difficult to do (for a sysop like me) but it requires consensus. You can propose it at MediaWiki talk:Common.js. Even better for maintainability is that such changes are made in wikibits.js. You can support my proposal to allow negative numbers and other features at bugzilla:8115.--Patrick 08:12, 18 June 2007 (UTC)

[edit] Not sorting properly

I have added the sm=n to the List of Aston Villa F.C. players article but the goals and appearances sections are still not listing properly. For the appearances section anyway it seems to be those numbers that are under 100. Can anyone help? Thanks Woodym555 14:11, 4 August 2007 (UTC)

Fixed now. I needed the nts template. Woodym555 18:22, 5 August 2007 (UTC)

[edit] Generic sorting template?

This question was moved here from the "Wikipedia-specific help" section in the help page:

I am trying to create number parameters for sorting. (Ex. sorting_table|table1|5|10, 5 being the number of rows and 10 being the number of colums). Is that even possible? I would like to be able to add or remove rows so a generic sorting template can be used for many different articles. The articles will have similar information but different names.

-- Ricky.Garcia 16:50, 7 August 2007 (UTC)

[edit] All demos work on meta but some fail on this copy

I just thought I should point out that as of time of writing some of the examples don't work in this version of the page (the initial sort is incorrect although second and subsequent sorts are correct) although they do on the meta original, so I guess something needs updating. --80.175.250.218 08:53, 9 August 2007 (UTC)

[edit] Sorting not functioning correctly

Can someone please look at the table here and check why the goal difference column does not sort correctly? Thanks in advance. Madcynic 10:06, 7 October 2007 (UTC)

Sorting a column with negative numbers works on Meta, where a bug was fixed, but the developers have not fixed it yet for MediaWiki in general (bugzilla:8115), and changes in MediaWiki:Common.js raise concerns about maintainability of the code. Anyway you would have to use a regular hyphen as minus sign. See also the version of the help page applicable without modifications of the JavaScript for workarounds.--Patrick 11:05, 7 October 2007 (UTC)

[edit] Another sorting anomoly

Hi! I recently created my first table, which a fellow editor converted to a sorting table. Problem is, we're seeing different things when we click the sort buttons! He's getting proper sorting in all columns, while only the first column sorts (ascending/descending) for me—regardless of which column's header I click on. Why would that be? The page in question is Wikipedia:WikiProject_Birds/Country_lists. I'm on Mac OSX10.4.10, using Safari. Help! MeegsC | Talk 13:10, 6 November 2007 (UTC)

I heard that before, I added that at m:Help:Sorting#Browser_issues.--Patrick 13:52, 6 November 2007 (UTC)

[edit] Hide someting

Hi,

I would like to know if it's possible to hide something in a sorting table.

Ex:

How I can sort byu last name

Name
Bob Dubois
Roger Smith
Frank Desmarais

Thank! Riba en (talk) 20:10, 9 December 2007 (UTC)

See Template:Sortname.--Patrick (talk) 00:37, 10 December 2007 (UTC)

[edit] Date sorting not working properly - please help

Could anyone tell me why this table is not sorting properly? We want to be able to sort by date. Various people over here have said that the sorting does not work for them. Thank you, Fang Aili talk 03:41, 16 December 2007 (UTC)

User Name Date of Original Request Last date page visited Wiki areas of interest Article areas of interest Requestor comments Contact log/Notes
Robotam (talk · contribs) 31 July 2007 Been active for some time, I believe I have given positive contributions, but I want to make sure I have not developed "tunnel vision." I would definitely appreciate wiki mentoring to do even more, and do it the right way.RoBoTamice 20:29, 31 July 2007 (UTC)
Lemonflash (talk · contribs) 1 August 2007 I'm an active contributer to Wikipedia, and wish to be an admin some day. Lemonflashtalk 01:03, 1 August 2007 (UTC)
Andyroo g (talk · contribs) 25 July 2007 I have wanted to be an admin for some time now, and would like to get some training to do so. I realise it is cliched, but i do truly have a misleadingly low edit count compared to my true amount of edits as a result of a large amount of edits whilst not signed in Andyroo g 18:46, 25 July 2007 (UTC)
Violask81976 (talk · contribs) 20 July 2007 Want some guidance. -Violask81976 15:15, 20 July 2007 (UTC)
geniussansom (talk · contribs) 16 July 2007 Would like some guidance on expanding the articles which I edit and would like to be an admin at some point in the future --geniussansom 15:34, 16 July 2007 (UTC)
Someguy1221 (talk · contribs) 11 July 2007 Looking to be an admin, and I think I might need some advice. Someguy1221 08:47, 27 April 2007 (UTC). Still interested. Someguy1221 07:08, 11 July 2007 (UTC)
Flubeca (talk · contribs) 10 July 2007 I want to become an admin :) -FlubecaTalk 19:02, 10 July 2007 (UTC)
Son (talk · contribs) 9 July 2007 I've been on en.wikipedia since December 2005 with some IP editing before that, and I'd like to become an admin. I definitely need some improvement, and I've gotten more invovled in XfDs and RfCs. --myselfalso 19:22, 9 July 2007 (UTC)
Nwwaew (talk · contribs) 23 June 2007 I want to see what I'm doing right, and what I need to improve to become an admin, and stand a chance at WP:RfA. This is what I think is the best course of action. Nwwaew (Talk Page) (Contribs) (E-mail me) 13:39, 23 June 2007 (UTC)
It's working exactly as it should: lexicographically. It sorts by day month year because that's the order you've written them. If you want to sort by date, write the dates as 2007-07-09 for example. Somewhere there's a template which displays one thing but sorts by another value. I'll look for it. —EncMstr 03:48, 16 December 2007 (UTC)
It appeared from the example on Help:Sorting that dates could be sorted without having to put them in the YYYY-MM-DD format. But I could not figure out what it was, or if I was misunderstanding completely. --Fang Aili talk 03:51, 16 December 2007 (UTC)
Use this instead: {{dts}}. Use it as {{dts|day|month|year}} in place of the date values. It produces an invisible sortable date and displays it formatted correctly according to user preferences. —EncMstr 03:54, 16 December 2007 (UTC)
Thanks, EncMstr. It looks like {{dts}} is my best bet. I really dislike introducing useless wikidate links, though. --Fang Aili talk 04:17, 16 December 2007 (UTC)
Don't think of them as links. Think of them as variables. That way I can see dates formatted the way I expect, and you can see what you expect via your preferences. —EncMstr 05:17, 16 December 2007 (UTC)

[edit] Sorting column with any negative numbers

When I have a column with a negative number, it sorts ok ascendingly, but sorts alphabetically descendingly. Is this a javascript problem? I've tried so many suggestions - none have worked --JimWae (talk) 08:30, 24 December 2007 (UTC)

  • Demographics of Queens e.g. - I've tried &Expression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operatorExpression error: Unexpected / operator-1.000000, removing % signs, even put a 0 in blank cell - no joy --JimWae (talk) 08:45, 24 December 2007 (UTC)
  • EVEN ascending is wrong on 1st click, OK on 3rd --JimWae (talk) 08:48, 24 December 2007 (UTC)

[edit] Date sorting (day/month)

I was hoping to use dates in a sortable table without specifying the year (i.e. 1 January), but I can't figure out how to do it and get the table to sort properly. I tried {{Dts}}, but year seems to be a requirement. Any help would be appreciated! PC78 (talk) 21:21, 22 February 2008 (UTC)

[edit] Rows/headers above sortable row

I'd like to add a header above the sortable row on the table in the Statewide polling article. Basically, above the first Clinton/Obama/Other columns I'd like to add a "Polling data" column (with colspan="3") and above the second Clinton/Obama/Other columns I'd like to add a "Delegates won" column (with colspan="3"). The "Date", "State", and "Delegates" column would ideally be modified to have rowspan="2". Can anyone help me do this? Andareed (talk) 22:28, 8 March 2008 (UTC)

[edit] Sorting numbers

Clicking the sort button messes up this table by sorting the numbers by digit rather than by number. How do I make it recognize that 10 is bigger than 2? Is there a parameter that makes it look at numbers differently? Thanks. --Arctic Gnome (talkcontribs) 04:50, 20 March 2008 (UTC)

Number of years
1 year
2 years
5 years
10 years
20 years
25 years
Never mind. I solved it by adding invisible zeros. I even found a template that does it for me: {{0}}. --Arctic Gnome (talkcontribs) 04:57, 20 March 2008 (UTC)
Another technique is to use the {{sort}} template:
Number of years
1 year
2 years
5 years
10 years
20 years
25 years
sort takes two parameters: {{sort|what to sort by|what to display}}. I thought it was smarter about accepting numbers, but to make the example work, I used leading zeros so a lexicographic sort works okay. Perhaps there's another form of the sort template which deals with numbers more intuitively. —EncMstr 05:00, 20 March 2008 (UTC)

[edit] Sorting when column items encompass multiple rows?

How can this be done? Example: Opinion polling for the United States presidential election, 2008 --Kallahan (talk) 21:11, 26 March 2008 (UTC)

Several solutions come to mind using the {{sort}} template. The daunting part is defining what sorting some of those columns should do. —EncMstr 01:34, 27 March 2008 (UTC)
Can someone make this sortable? Kallahan (talk) 22:47, 30 March 2008 (UTC)
Poll Source Date administered Democrat  % Republican  %
Rasmussen Reports Tracking March 25-28, 2008 Hillary Clinton 40% John McCain 51%
Barack Obama 43% John McCain 48%
Gallup Tracking March 24-28, 2008 Hillary Clinton 44% John McCain 48%
Barack Obama 44% John McCain 46%
NBC News/Wall Street Journal March 24-25, 2008 Hillary Clinton 44% John McCain 46%
Barack Obama 44% John McCain 42%
Pew Research Center March 19-22, 2008 Hillary Clinton 49% John McCain 44%
Barack Obama 49% John McCain 43%
Fox News/Opinion Dynamics March 18-19, 2008 Hillary Clinton 46% John McCain 43%
Barack Obama 43% John McCain 44%
CBS News March 15-18, 2008 Hillary Clinton 46% John McCain 44%
Barack Obama 44% John McCain 46%
Does this mean it can't be done? —Preceding unsigned comment added by Kallahan (talkcontribs) 18:06, 1 April 2008 (UTC)
Well, the straightfoward change appears okay at first (below), but a click on any of the sort handles makes it fall apart immediately.
Poll Source Date administered Democrat  % Republican  %
Rasmussen Reports Tracking March 25-28, 2008 Hillary Clinton 40% John McCain 51%
Barack Obama 43% John McCain 48%
Gallup Tracking March 24-28, 2008 Hillary Clinton 44% John McCain 48%
Barack Obama 44% John McCain 46%
NBC News/Wall Street Journal March 24-25, 2008 Hillary Clinton 44% John McCain 46%
Barack Obama 44% John McCain 42%
Pew Research Center March 19-22, 2008 Hillary Clinton 49% John McCain 44%
Barack Obama 49% John McCain 43%
Fox News/Opinion Dynamics March 18-19, 2008 Hillary Clinton 46% John McCain 43%
Barack Obama 43% John McCain 44%
CBS News March 15-18, 2008 Hillary Clinton 46% John McCain 44%
Barack Obama 44% John McCain 46%
I'm at a loss how to remedy that, though this comes to mind:
Poll Source Date administered Democrat  % Republican  %
Rasmussen Reports Tracking March 25-28, 2008 Hillary Clinton
Barack Obama
40%
43%
John McCain 51%
Gallup Tracking March 24-28, 2008 Hillary Clinton
Barack Obama
44%
44%
John McCain 48%
46%
NBC News/Wall Street Journal March 24-25, 2008 Hillary Clinton
Barack Obama
44%
44%
John McCain 46%
42%
Pew Research Center March 19-22, 2008 Hillary Clinton
Barack Obama
49% John McCain 44%
43%
Fox News/Opinion Dynamics March 18-19, 2008 Hillary Clinton
Barack Obama
46%
43%
John McCain 43%
CBS News March 15-18, 2008 Hillary Clinton
Barack Obama
46%
44%
John McCain 44%
46%
Probably unsuitable, even if my mistakes were fixed.... —EncMstr 18:39, 1 April 2008 (UTC)
Actually that's brilliant. I'm going to propose it and credit you for the idea. Thanks! --Kallahan (talk) 20:25, 1 April 2008 (UTC)

[edit] Can this be got working?

Vehicle fuel consumption max. speed max. power
l/100 km mpg (imp) mpg (US) km/h mph kW hp
my car 12 24 20 160 100 300 400
your car 20 14 12 120 75 150 200
his car 24 12 10 800 500 900 1,200
her car 10 29 24 80 50 90 120

In the header, it would be nice if the sort thingies were just where they're not & not where they are. Jɪmp 08:32, 27 March 2008 (UTC)

Like this?
Vehicle Fuel Consumption, Speed and Power
Vehicle l/100 km mpg (imp) mpg (US) km/h mph kW hp
my car 12 24 20 160 100 300 400
your car 20 14 12 120 75 150 200
his car 24 12 10 800 500 900 1,200
her car 10 29 24 80 50 90 120
199.125.109.19 (talk) 22:28, 31 March 2008 (UTC)

[edit] Define column to be sorted by default

when i have !Name!!Surname!!Height: Name is sorted by default. Now I'd like to have Surname sorted by default but still as the second column. Is it possible? --Subfader (talk) 20:35, 27 March 2008 (UTC)

I think it does not sort by default: it displays the original page in the order the wikitext is arranged. So to have it default sorted, just rearrange the wikitext. —EncMstr 21:00, 27 March 2008 (UTC)
I had a similar need on our internal documentation wiki and having to manually sort the wiki text was not feasible in my case. It's a table of project statuses that I wanted sorted by the status column. Every time a project status changed (dev->qa for example), that meant copying/pasting the row and trying to manually keep everything in the right spot... yuck. Anyway, enough of my story. I've been fiddling with the wikibits.js and made some changes that seem to be working for us (additions in bold):
 function ts_makeSortable(table) {
   var firstRow;
 
   var reverseSort = false;        // flag indicating whether it should be reverse order
   var defaultSortAnchor = null;   // anchor tag that is the sort control for the default sort column
 
   if (table.rows && table.rows.length > 0) {
     if (table.tHead && table.tHead.rows.length > 0) {
       firstRow = table.tHead.rows[table.tHead.rows.length-1];
     } else {
       firstRow = table.rows[0];
     }
   }
   if (!firstRow) return;
 
   // We have a first row: assume it's the header, and make its contents clickable links
   for (var i = 0; i < firstRow.cells.length; i++) {
     var cell = firstRow.cells[i];
     if ((" "+cell.className+" ").indexOf(" unsortable ") == -1) {
 
       cell.innerHTML += '  <a href="#" class="sortheader" onclick="ts_resortTable(this);return false;"><img src="'+ ts_image_path + ts_image_none + '" alt="↓"/></a>';
 
       // if the class contains "defaultsort" then sort on this column
       if ((" "+cell.className+" ").indexOf(" defaultsort ") > -1) {
 
         // look for the "sortheader" anchor tag and use it as the sort control
         var cellChildren = cell.childNodes;
         for( var j = 0; j < cellChildren.length; j ++ ) {
           if( cellChildren[j].nodeName == "A" && cellChildren[j].className == "sortheader" ) {
             defaultSortAnchor = cellChildren[j];
           }
         }
 
         // if the class contains "reverse" then do a reverse sort
         if ((" "+cell.className+" ").indexOf(" reverse ") > -1) {
           reverseSort = true;
         }
       }
 
     }
   }
   if (ts_alternate_row_colors) {
     ts_alternate(table);
   }
 
   // if we found a default sort anchor, then do the sort
   if( defaultSortAnchor != null ) {
     ts_resortTable( defaultSortAnchor );
     if( reverseSort ) {
       ts_resortTable( defaultSortAnchor );
     }
   }
 
 }

With this change, all you need to do to make a column sortable is put class="defaultsort" in the header cell of the column you want to sort on (note: if you have this on multiple columns, the last one wins). You can also add the "reverse" class if you want it to reverse sort. This is probably an inelegant solution but it's working great for us. --Aylwyne (talk) 12:39, 6 June 2008 (UTC)

[edit] Slow - what method?

Sorting 1,000 or so items is very slow (example: list of chess grandmasters). What method does the sorting use? Could it be faster? Bubba73 (talk), 22:35, 30 March 2008 (UTC)

It uses http://en.wikipedia.org/skins-1.5/common/wikibits.js containing in "newRows.sort(sortfn)" the sort function of Javascript itself. Thus the method used could depend on the browser.--Patrick (talk) 22:51, 30 March 2008 (UTC)
If the input list was initially sorted, would that make it come up faster? Bubba73 (talk), 23:01, 30 March 2008 (UTC)
JavaScript sorting is not done initially, but only after pressing the sort link, so I do not understand what you mean. Of course, putting the wikitext in the most commonly desired order is useful because it saves doing any JavaScript sorting to get that order.--Patrick (talk) 23:56, 30 March 2008 (UTC)
When I go to list of chess grandmasters in IE7, it takes a long time for it to come up initially (but not so long in Firefox). I was wondering if it was initially running the sort, which is taking a long time. But if it doesn't sort until someone click, then that can't be it. Is there anything simple that can be done to make that article come up faster? Bubba73 (talk), 15:02, 31 March 2008 (UTC)
The slow load time is a consequence of all the flag templates. Remove them and it will be much faster. — Andrwsc (talk · contribs) 20:36, 1 April 2008 (UTC)
 ::::: I thought that might be the case, and it was discussed on the article's talk page, but someone did a test and they found that the flags were not the problem. (See list of chess grandmasters talk page.) Bubba73 (talk), 20:43, 1 April 2008 (UTC)
It's not clear from the talk page discussion what browsers were used for that test, but it is a known problem that special code added to MediaWiki:Common.js for PNG images (which is how flag icons are thumbnailed on server caches) slows IE page loads. In any case, I think you might be better off without flags on that page anyway, for WP:Manual of style (flags) issues. — Andrwsc (talk · contribs) 21:04, 1 April 2008 (UTC)
Right. I first noticed the slowness problem with IE7. Then when I tried Firefox it was much faster. Thanks for the MoS link - I had already questioned whether there should be flags or not. Bubba73 (talk), 22:30, 1 April 2008 (UTC)

[edit] static columns

Is there a way of disabling sorting on one or more columns? I see the static columns example but it's not ideal building it with two separate tables.

Anyone? Cnbrb (talk) 21:50, 9 April 2008 (UTC)

I don't think there's any supported method. There's probably some way to do it by hiding the handles, but I don't know how. Why do you want to disable sorting some columns? —EncMstr 22:07, 9 April 2008 (UTC)

Thanks - sometimes I have a table where a reader might want to sort (say) column 1 alphabetically but subsequent columns might not be something worth sorting (e.g. a notes column). Hiding the handles would be the kind of thing I was thinking of. Cnbrb (talk) 23:15, 9 April 2008 (UTC)

Even sorting a notes column is useful to collect the rows which have a note entry—and the same note. If that seems haphazard, revising the note entries to make them more friendly, perhaps with standard leading keywords or tags. Granted, there are a few tables where the columns make little sense to sort, but that's hardly a reason to prevent it. Perhaps someone, somewhere has a use. —EncMstr 23:38, 9 April 2008 (UTC)

[edit] Requesting to view wikitable code for Help:Sorting#Examples

I'd like to view the code for several examples used at Help:Sorting#Examples However, that page is protected from editing. Is there another location where the code is able to be viewed?

Specifically, I would like to see the wikitable code for the very last example which should show how to correctly use the "sm=n" solution for forcing numerical sort.

Quote:

  • Sortable version of Template:Climate in Middle East cities, using smn to specify numeric sort mode

Najevi (talk) 15:09, 20 April 2008 (UTC)

Ha! Found the source code at http://meta.wikimedia.org/wiki/Help:Sorting#Examples

| style="text-align:center;" |-3°C{{smn}}
| style="text-align:center;" |7°C{{smn}}

Najevi (talk) 15:21, 20 April 2008 (UTC)

[edit] Only some of the numbers will sort

In the article International wheat production statistics, years 2002-1999 work fine, while years 1998-1996 don't sort correctly. Is there any way to fix this, or is this a known "bug"? Esn (talk) 04:49, 23 April 2008 (UTC)

Hold on, I think I see what is causing it. The problem columns all have the entry "0NA" (signifying "negligible" or "not on the list in that year"). Is there any way to say "n/a" in a table without saying "0" (which wouldn't technically be correct)? Esn (talk) 04:53, 23 April 2008 (UTC)

[edit] Please help: Sortable table provides one round of correct sorting, another of incorrect sorting

Try this: visit the birth control comparison table, and click the button to sort by "perfect use" rates, once. Only once. See the two "no data" items at the top, there? OK, now click it again: what's happening is that it is being sorted in a different way. I don't know the name for it, but if you look at the very top of the block of blue items in "perfect use", and start moving your eyes up line by line, you will see it go from 2 to to 20 to 26. Clearly it is being sorted in a way that make mathematical sense, but is useless for that table's intended comparison. Is there a way to make the sortable table not do this? Thanks! Whistling42 (talk) 02:45, 25 April 2008 (UTC)

Interesting. I speculate that it determines the type of comparison (string or numeric) to use by looking at the first element in the table. Once it's sorted so that "no data" is on top, it then uses a string comparison. To fix, use the {{sort|<what to sort by>|<what you see>}} template. For the no data, I expect the sort key would be 0 (zero). —EncMstr (talk) 02:53, 25 April 2008 (UTC)
Thanks for your response. I understand, somewhat, but I don't know what you mean by "first element in the table". It is definitely switching between numeric and string. I'm not really sure how to use the template; in my previews (using the Sort template) using 0, 100, and a blank space, it still didn't work properly. Any ideas? Care to tinker with it? The only method I found that works is to remove the "no data" and "see note" text entirely, but I would rather keep the text if there is a way. Whistling42 (talk) 03:42, 25 April 2008 (UTC)
This diff shows the changes I intended. I keep meaning to look up how to make {{sort}} not use a string comparison, but I just padded it with leading zeros and made it work okay. If you don't like "no data" sorting as though its value is zero, it should be clear now how to change that. —EncMstr (talk) 04:08, 25 April 2008 (UTC)

[edit] Error on Mediawiki version ?

On the help page says "This works in MediaWiki 1.9 and above", but on MediaWiki, the las version is: "2008-03-20: 1.12.0 quarterly release". So we should wait some month for 1.9 version, and all Mediawiki projects has a very unstable version!? —Preceding unsigned comment added by 190.18.45.41 (talk) 02:22, 14 May 2008 (UTC)

The versions are sorted weirdly. Version 1.12.0 is "above" 1.9, that is, 1.12.0 was released after 1.9. You might check out mw:Version lifecycle for information on the releases. JackSchmidt (talk) 04:55, 14 May 2008 (UTC)