Page MenuHomePhabricator

Sorting mixed content in tables: numbers first
Closed, DeclinedPublic

Description

Sorting in tables can be allowed with the sortable-option.
Tablesorter.js [https://phabricator.wikimedia.org/diffusion/MW/browse/master/resources/src/jquery/jquery.tablesorter.js] tries to select a way of sorting if this is not specified by the writer of the code.
Tablesorter does, if (auto)-selected parser = 'number', assign a zero-value to non-numerical input (isNaN).
In a number of lists, this is an exceptable choice, in others it would be better to have the non-numerical input listed below any numerical input.

I would like to have an extra Parser, which gives a MAX_VALUE to all non-numerical input, or (even better, but I didn't figure out how) have the non-numerical values sorted below the numerical values.

A possible solution would be the adding of two functions to [https://phabricator.wikimedia.org/diffusion/MW/browse/master/resources/src/jquery/jquery.tablesorter.js]:

formatDigitnf, similar to formatDigit, but with MAX_VALUE for isNaN
<code>

formatDigitnf: function ( s ) {
  var out, c, p, i;
  if ( ts.transformTable !== false ) {
    out = '';
    for ( p = 0; p < s.length; p++ ) {
      c = s.charAt( p );
      if ( c in ts.transformTable ) {
        out += ts.transformTable[ c ];
      } else {
        out += c;
      }
    }
    s = out;
  }
  i = parseFloat( s.replace( /[, ]/g, '' ).replace( '\u2212', '-' ) );
  return isNaN( i ) ? MAX_VALUE : i;

},
</code>

extra Parser
<code>
ts.addParser( {

  id: 'numberfirst',
  is: function ( s ) {
    return $.tablesorter.numberRegex.test( $.trim( s ) );
  },
  format: function ( s ) {
    return $.tablesorter.formatDigitnf( s );
  },
  type: 'numeric'
} );

</code>

Examples of usage:
[https://nl.wikipedia.org/wiki/Formule_1_in_1987#Uitslagen_coureurs], having not qualified/started/finished drives below those who did finish
[https://nl.wikipedia.org/wiki/Lijst_van_Radio_2-Top_2000's] having not-noted songs below those who did get noted

Event Timeline

RonnieV created this task.Jan 12 2016, 11:23 AM
RonnieV raised the priority of this task from to Needs Triage.
RonnieV updated the task description. (Show Details)
RonnieV added a subscriber: RonnieV.
Restricted Application added subscribers: StudiesWorld, Aklapper. · View Herald TranscriptJan 12 2016, 11:23 AM
Aklapper set Security to None.
TheDJ closed this task as Declined.EditedJan 21 2016, 9:44 AM
TheDJ claimed this task.
TheDJ added a subscriber: TheDJ.

Use the data-sort-value attribute on a cell to specify a 'fake' number instead.

RonnieV added a comment.EditedJan 21 2016, 9:57 AM

I'd like to ask to reconsider this decline.

On a page like https://nl.wikipedia.org/wiki/Lijst_van_Radio_2-Top_2000's, replacing 17*2094 times <nowiki>-</nowiki> and <nowiki>*</nowiki> with something like <nowiki>data-sort-value="9999"| -</nowiki> and <nowiki>data-sort-value="9999"|*</nowiki>does get a lot of resistance, as this would (more than) double the size of the page (from 600kb to over 1.4 MB).

Inserting this code isn't a problem, the loadweight of the page definitively is.

More, through lighter, tables could benefit from this small function in the common.js, which allows a more natural sorting.