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.

Raghavendra Habbu :

Its a great script. But let me know where to set TableSortNoAutoStart = True

Raghavendra Habbu :

Its a great script. But let me know where to set TableSortNoAutoStart = True

jalil :

excellent.

i need a table sorter, found a few but installing them is painful.

i found yours, slipped in the url for the js, and it works instantly. i'd like other to know i recommend this work very favourably.

many thanks.

bhupendra atre :

Thanks for having the js sorting. There are a lot of ones out there but I liked the ability to call the sort method rather than not exposing the api. I tested your code and found that if my table has alternate row background color, then the sorting messes up the background. Did you get any time to work on it by any chance? Would apreeciate if the code worked for alternate row highlighting too.

Marcin Pyć :

Hello!
I looking for advice about sorting in the table by column.
I have search a lot of JS sorting scripts and there are information about sorting by number or name.
Look at my table, now its looking like this: http://www.wklej.org/id/23873/
I would like to sort like here:
General sorting like under, but it’s used not only when new rows are added (column IM isn’t sorted, because in each rows, there is constant number , after are column sorted of Stan. ((S – it’s constant, and numbers are for 1 do 7, where e.g. S1 is might to have a few Nr op., S2 is might to have a lot of Nr op. etc)).
After Nr op column is sorted (there are – three digital numbers and behind it will be 3 liters but EL).

1) Column sort by key of TKO ( from the biggest value to least)

2) Sort e.g. by key of KD=B- it is sorting all rows, where is letter B in KD column (like this for D and KT)

Sorting by KD=B key:

15 S2 0 020C Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww B
15 S5 0 005EL Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww B
15 S7 0 005 Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww B

Sorting by KD=D key:

15 S2 1 005 Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww D
15 S2 0 010 Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww D
15 S3 0 005BEL Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww D
15 S4 1 005 Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww D
15 S6 1 005D Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww D

Sorting by KD=D key:

15 S1 0 005 Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww KT
15 S1 0 020EL Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww KT
15 S2 2 015EL Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww KT
15 S6 PP 010 Wwwwwwwwwww wwwwwwwwwwwwww wwwwwwwwwwwww KT

This above is typical sorting of this table, but on the table will be more rows.
(Of course it might are sorting 20T3STD020103001Z5K1 or 986-23-2346 F5)

I looking for help. Its very important for me. Please if you know how to do it help me.

Greatings

Marcin Pyć

Chetan :

How to do sort in the situation,in which 2 or more rows having common <td> that is in this case rowspan increases .
plz Suggest .

svend :

Thanks, this is great!

 

comment on this