Sort array with TableSorter like functions. #81

vkostas opened this Issue May 30, 2012 · 8 comments


None yet
2 participants

vkostas commented May 30, 2012

I came through this nice plugin and started to use it.
I am building html pages of my media collection but I am running into performance issues. I build the output data and put in an 2 dimensional [200k x 40] Array within HTML file. Output table is build based on the literal data.
I find (almost) useless to build a HUGE table (8M cells) with all data then let tablesorter sort the whole thing and then start paging to show only a small part of it.
Sorting functionality is very clever but limited only to table elements.
Is there any way to use TableSorter functionality to prepare (sort) the array first? Performance will be boosted for sure.
Maybe I am doing something wrong? Something escaped my attention?
Thank you in advance.


Mottie commented Jun 1, 2012

Hi vkostas!

I like the idea, but my concern would be that a basic table would need to be set up to build the table.

This is close to what the initialize tablesorter on an empty table demo does, but as you said you'd have to build a table first.

I guess I could write an extension or something that will populate the internal table cache from an array, but it would still need to parse the data for the appropriate column, so it may not be as fast as you think.

vkostas commented Jun 2, 2012

If you think this could be useful for others then I think it is worth giving it a try.
In general would be very very useful a part of internal functionality can be used prior to HTML table.
E.g. replaceAccents(). I wanted to put a sorted value as attribute of a column so parser could locate it easily but couldn't call tablesorter.replaceAccents().
In fact this function could officially support Greek letters ΑΕΙΟΥαειου (I can help on it off course).


Mottie commented Jun 3, 2012

You can use the replace accents function outside of tablesorter:

var a = $.tablesorter.replaceAccents('Béjaïa'); // "Bejaia"

And sure if you want to share the code for geek letter support, I would appreciate it. I actually think I'll add a wiki page to show how to extend the $.tablesorter.characterEquivalents for different languages instead of including it in the code because there might be some interference. For example, ß is currently treated as an equivalent to S.

As for the array idea... I'll look into it.

vkostas commented Jun 3, 2012

Hi Mottie,

I already had tried that code but didn't work. That's why I wrote here.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Test replaceAccents</title>
<script type="text/javascript" src="../jquery/js/jquery.js"></script>
<script type="text/javascript" src="../jquery/js/jquery.tablesorter.js"></script>
<script type="text/javascript">
var s = $.tablesorter.replaceAccents('ë');
document.write('.........' + s + '.........');

Error message "this.characterRegex is undefined" is raised from above code.
I have a hidden sorted value for several columns. I want to include sorted value as cell attribute (e.g. internal="...."). replaceAccents() should be called before tablesorter starts "extending" the HTML table.
What I am doing wrong?

Thank you for both issues.


Mottie commented Jun 3, 2012

Ahhh ok, the characterRegex needs to be run first, i.e. set up a table first, before you can use the $.tablesorter.replaceAccents() utility. I'll fix that.


Mottie commented Jun 3, 2012

Ok, the replaceAccents function won't give you an error anymore.

I'm going to have to put this enhancement on a backburner for now.. at least until I've caught up fixing problems in some of my other projects.

vkostas commented Jun 3, 2012

You deserve a great THANK YOU!!!
I confirm replaceAccents() works fine.
Please take your time for the enhancement.


Mottie commented Nov 27, 2013

Sorry, I should have updated this issue a while ago... basically, you can sort using any of the built-in sorting functions as follows:

The following sort functions are currently available in v2.14+:

  • $.tablesorter.sortNatural - plain natural sort (ascending only)
  • $.tablesorter.sortNaturalAsc - sort empty strings + plain natural sort (ascending only)
  • $.tablesorter.sortNaturalDesc - sort empty strings + plain natural sort (descending only)
  • $.tablesorter.sortText - plain text sort
  • $.tablesorter.sortNumeric - plain numerical sort (ascending only)
  • $.tablesorter.sortNumericAsc - sort empty strings + text + plain numeric sort (ascending only)
  • $.tablesorter.sortNumericDesc - sort empty strings + text + plain numeric sort (descending only)

Use any of the above as follows:

var array = [ /* a filled array */ ];
// ascending plain natural sort
array.sort(function(a,b) { return $.tablesorter.sortNatural(a,b); });

// descending plain natural sort
// array.sort(function(a,b) { return $.tablesorter.sortNatural(b,a); });

Mottie closed this Nov 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment