Skip to content
:arrow_up_down: A small tablesorter in vanilla js
JavaScript HTML CSS
Find file
Latest commit c630976 Mar 30, 2016 @tristen v4.0.1


A small & simple sorting component for tables written in Javascript.

npm version Build Status

Quick start

<script src='tablesort.min.js'></script>

<!-- Include sort types you need -->
<script src='tablesort.number.js'></script>
<script src=''></script>

  new Tablesort(document.getElementById('table-id'));

Browser Support

Tablesort expects classList to be supported. For this to work on older versions of IE, use a shim.

Chrome logo Firefox logo Internet Explorer logo Opera logo Safari logo
8+ ✔ 3.6+ ✔ 10+ ✔ 11.50+ ✔ 5.1+ ✔

Sort Types

See all available sort types in the sorts directory. Defaults to string if no sort types are provided.

Additional options


You can pass in options as a second parameter. Currently one option is supported: descending: true. By default, sort is set to ascending.

new Tablesort(document.getElementById('table-id'), {
  descending: true

To override the sort order of a particular column, a data-sort-order attribute can be added to its th element. Accepted values are asc for ascending and desc for descending.

<th data-sort-order='desc'>Name</th>

Exclude columns or rows

For columns or rows that do not require sorting, you can add a class of no-sort to a columns th or a tr element.

<th class='no-sort'>Name</th>

<tr class='no-sort'>
  <td>Gonzo the Great</td>

Override data that is sorted on

Sometimes text inside cells is not normalized. Using a data-sort attribute you can use optional data to sort on.

  <td data-sort='1357656438'>01/08/13 @ 8:47:18am EST</td>
  <td data-sort='1078673085'>3/7/2004 @ 9:24:45 EST</td>

Specify the sort method for a column

By adding a data-sort-method attribute to a table heading you can force Tablesort to use a specific sorting method rather than guessing it. The value of data-sort-method corresponds to the name of a sort function.

<th data-sort-method='dotsep'>Version</th>

Default sort on tablesort initialization

It is possible to automatically sort the table once you create a Tablesort instance by adding sort-default class.

<th class='sort-default'>Name</th>


Tablesort supports two custom events: beforeSort & afterSort.

var table = document.getElementById('table-id');
var sort = new Tablesort(table);

table.addEventListener('beforeSort', function() {
  alert('Table is about to be sorted!');

table.addEventListener('afterSort', function() {
  alert('Table sorted!');

Refresh sort on appended data

Tablesort supports sorting when new data has been added. Simply call the refresh method.

var table = document.getElementById('table-id');
var sort = new Tablesort(table);

// Make some Ajax request to fetch new data and on success:

See homepage for example


// npm install tablesort
var tablesort = require('tablesort');

tablesort(el, options);

Ender support

Add tablesort as an internal chain method to your Ender compilation.

// ender add tablesort


Default CSS

Add the styling below to your CSS or roll with your own.

th.sort-header::-moz-selection { background:transparent; }
th.sort-header::selection      { background:transparent; }
th.sort-header {
th.sort-header::selection {
table th.sort-header:after {
  border-width:0 4px 4px;
  border-color:#404040 transparent;
table th.sort-header:hover:after {
table th.sort-up:after,
table th.sort-down:after,
table th.sort-down:hover:after {
table th.sort-up:after {
  border-width:4px 4px 0;

Extending Tablesort

If you require a sort operation that does not exist in the sorts directory, you can add your own.

Tablesort.extend('name', function(item) {

  // Regular expression to test against.
  // `item` is a table value to evaluate.
  return /foo/.test(item);
}, function(a, b) {

  // Custom sort functionality goes here.
  // e.g var n = (a > b) ? -1 : 1;
  return n;

If you've made an extend function that others would benifit from pull requests are gladly accepted!


Tablesort relies on Grunt as its build tool. Simply run grunt to package code from any contributions you make to src/tablesort.js before submitting pull requests.

Tests are run via:

npm install && npm test




Create an issue

Something went wrong with that request. Please try again.