Last modified: 2012-08-13 22:19:44 UTC
The current tablesorter algorithm is "unstable": it does not preserve (or sort consistently) the relative order of rows where the cell content is equal. For example, in the enwiki:VPT example linked below, clicking the "Col 1" sort button twice in the table below should preserve the order of rows 2 to 8 (or invert them then restore them). But instead, the tie-break rows sort pseudorandomly (returning to the orginal order after 6 clicks instead of 2). It is confusing for tables to change the order of rows with identical sort values. Since Wikipedia articles are not expected to contain extremely large tables, computational optimisation should be less important than intuitive operation. The current jquery.tablesorter.js claims to use a merge sort algorithm, but apparently not a stable one. At present, enwiki is at version 1.19wmf1 (r114429). I do not know whether this also affects versions prior to 1.19. Live JavaScript: http://en.wikipedia.org/w/resources-1.19/resources/jquery/jquery.tablesorter.js SVN: http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/resources/jquery/jquery.tablesorter.js I think Bug 31255 is a different but similar issue. Example at: http://en.wikipedia.org/wiki/Wikipedia:Village_pump_(technical)#Unstable_tablesorter_algorithm Wikitext of example table: {| class="wikitable sortable" ! Col 1 !! Col 2 !! Col 3 |- | aa || aba || 1 |- | ab || abb || 2 |- | ab || abc || 3 |- | ab || abd || 4 |- | ab || abe || 5 |- | ab || abf || 6 |- | ab || abg || 7 |- | ab || abh || 8 |- | ac || abi || 9 |}
Changing summary to reflect the main issue raised (the intent to maintain original order when reversing the order twice)
Not only should the re-sorting be reversible; there should be consistent tie-breaking on each click. A stable and intuitive tie-breaker (fallback secondary sort key) would be any one of: * the original row order * the current row order * the sort order of content in other columns. Clicking to reverse a column's sort order could either maintain the tie-breaker or reverse it. But whichever approach is chosen, there should be an implicit tie-breaker (or, equivalently, a stable sort algorithm) so that rows do not move to counterintuitive or inconsistent positions. Example moved to http://en.wikipedia.org/wiki/User:Richardguk/Unstable_tablesorter_algorithm
Fix proposed by Anomie at Gerrit change #15638
patch merged. seems to be resloved.