Page MenuHomePhabricator

jquery.tablesorter should preserve original order of equal cells when reversing sort order twice
Closed, ResolvedPublic


Author: richardg_uk

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:


I think Bug 31255 is a different but similar issue.

Example at:

Wikitext of example table:

{| class="wikitable sortable"
! Col 1 Col 2 Col 3


Version: unspecified
Severity: normal



Event Timeline

bzimport raised the priority of this task from to Low.Nov 22 2014, 12:19 AM
bzimport set Reference to bz35526.
bzimport added a subscriber: Unknown Object (MLST).

Changing summary to reflect the main issue raised (the intent to maintain original order when reversing the order twice)

richardg_uk wrote:

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

richardg_uk wrote:

Fix proposed by Anomie at Gerrit change #15638

patch merged. seems to be resloved.