table sort

23 October 2006 // javascript. things.

No, your eyes don't deceive you, this is Yet Another Javascript Table Sort script, designed to be unobtrusive, simple and flexible... (skipped 1000 lines of happy talk ;)

 

LoginPointsCreated atUS DateGerman DateMoneyImage
Blythe50Wed, November 24, 200404/12/033.9.05$6,89img20.png
Edmund104Fri, January 23, 200403/20/0310.1.05$87,75img27.png
Jonquil12Sat, January 22, 200504/15/0330.8.04$1.239,14img1b.png
Lee55Fri, March 11, 200506/03/054.5.05$41,74img129.png
Sky33Fri, May 27, 200502/02/0522.9.04-$8,92img15.png
Archibald75Tue, January 18, 200501/14/0423.2.04--img10a.png
Emma70Tue, August 23, 200501/14/0501.05.03$6,64img.png
Erin92Tue, August 9, 200508/27/0301.03.04$12,77image
Edgar25Fri, January 2, 200412/22/0312.06.05$56,20img1.png
Prudence85Thu, April 29, 200405/13/0415.8.03-$37,62img10.png
Walter50Sun, March 16, 200303/27/0416.7.05$1.234,56 
Reginald81Sat, March 8, 200310/07/0421.3.05$74,74img3.png
Mercy50Thu, June 17, 200409/04/0330.07.05$75,96img1.png
Holly60Tue, August 23, 200512/14/0410.03.04$53,77not found
Cedric76Mon, June 28, 200408/18/0413.1.03$1,48img1a.png

download js file

usage

<script src="tablesort.js"></script>

that's all about it. ;)

By default, the script starts automatically at page load and makes all tables in document sortable. To disable autostart, set TableSortNoAutoStart to true before including the script.

As usual, your tables should be divided into THEAD and TBODY, you also need to declare three css classes for table header cells, for example:

/* indicates that the column can be sorted */
td.sortable
    { background:#606060; font-weight: bold; cursor: pointer; }

/* indicates that the column is sorted from low to high */
td.sorted_asc
    { background:#606060 url(up.gif) center right no-repeat; }

/* indicates that the column is sorted from high to low */
td.sorted_desc
    { background:#606060 url(down.gif) center right no-repeat; }

data types

TS can handle different kinds of data in table columns. The built-in sorting rules are

  • nocase -- case-insensitive sort (default)
  • alpha -- case-sensitive sort
  • numeric -- 123 or 123.5 or 1e+3
  • natural -- natural sort (like Image column in the table above)
  • currency -- currency values like $0,55 or 1,234.56€
  • date -- dates (in any format javascript parse can handle)
  • usdate -- US dates (mm/dd/yyyy)
  • eudate -- German dates (dd.mm.yyyy)

TS is able to detect most data types based on the contents of the first cell in the column.

api

For javascript programmers among us ;) TS provides two public functions: enable() and sort() . The prototype of enable() is as follows

TableSort.enable(table, column, compare)

This turns on sorting of table table on column column using compare as comparison function (well, was it really necessary to say that? Good programs don't need any documentation, do they? ;) Anyways,

  • table is either an id or DOM table object
  • column is a column number (starting from 1) or title (without any html)
  • compare is one of the rules listed above (in quotes) or a pointer to your own comparison function (as in sort)

If an argument is omitted (use '' or null as placeholder when appropriate), TS uses the following defaults:

  • if table is omitted, it takes all tables (with THEADs)
  • if column is omitted, all columns will be sortable
  • if compare is omitted, TS does its best to guess the correct datatype for the column

Examples:

// sort all columns in all tables using default comparison rules
TableSort.enable()

// sort 'Points' (the second) column in the 'people' table
// using default comparison rule
TableSort.enable('people', 'Points')

// same as above
TableSort.enable('people', 2)

// same as above
TableSort.enable(document.getElementsByTagName('TABLE')[0],  2)

// sort 'Created at' using 'human date' comparison
TableSort.enable('people', 'Created at', 'date')

// sort 'Product Number' in all tables using custom comparator
TableSort.enable('', 'Product Number',  MyDomain.compareProductNumbers)

TS handles events (clicks) on its own, it is also possible to call the sorting function from your code. That's what the sort method is for.

TableSort.sort(table, column, compare)

The parameters are as above, but table and column are required.

previous work

This script is based on the works of Paul Sowden and Stuart Langridge. There is also a plenty of similar scripts already written...

 
If you think this comment is spam or otherwise completely irrelevant here, feel free to hide it. The comment disappears immediately, though it is not deleted, so I have an option to "unhide" it later.
 

comment on this