plastic cloud plastic cloud - 2 months ago 20
Javascript Question

How to sort an existing table in greasemonkey?

I'm writing a greasemonkey user.js for a page with a table in it. (Table is 100 rows by 18 columns.) Now what I want to do is to make it sortable on column, and also have it run in both Chrome and Firefox.

All searches for answers so far resulted in suggestions to use jquery/dojo or something alike.

Can I be done without any external code? Ofter all it's just a matter of replacing the row's in a different order, right? or is that a silly thing to say?

The thing is that I'm already using dojo for some query needs but since I want it to run in both Firefox and Chrome, I just copy paste the whole dojo thing in my script..

Also, most of the solutions I found so far seem to be more for use when building a table, not for altering an existing one.

Any help is appreciated.

EDIT: All cells in the table contain numbers.


The smart way is to use a tool like tablesorter.
But, since you don't want to use external code (for now), it can be done the hard way.

Here's how to do it the semi-hard way. Note that I AM using jQuery. You'd be smart to incorporate at least that into your script.

Go ahead and use the // @require directive. You can still run the GM script in Chrome using the Tampermonkey extension.
(There are other ways of including jQuery in GM scripts under Chrome, as well.)

Anyway, code like so: will do the trick:

//--- Get the table we want to sort.
var jTableToSort    = $("table#myTable");

//--- Get the rows to sort, but skip the first row, since it contains column titles.
var jRowsToSort     = jTableToSort.find ("tr:gt(0)");

//--- Sort the rows in place.
jRowsToSort.sort (SortByFirstColumnTextAscending).appendTo (jTableToSort);

function SortByFirstColumnTextAscending (zA, zB)
     var ValA_Text  = $(zA).find ("td:eq(0)").text ();
     var ValB_Text  = $(zB).find ("td:eq(0)").text ();

     if      (ValA_Text  >  ValB_Text)
        return 1;
     else if (ValA_Text  <  ValB_Text)
        return -1;
        return 0;

You can see it in action at jsFiddle.


To sort numbers, you would use a sorting function like:

function SortBy2ndColumnNumber (zA, zB)
   var ValA = parseFloat ($(zA).find ("td:eq(1)").text () );
   var ValB = parseFloat ($(zB).find ("td:eq(1)").text () );

   return ValA - ValB;

See number sorting in action at jsFiddle.