Click here to Skip to main content
13,199,685 members (59,128 online)
Click here to Skip to main content
Add your own
alternative version


76 bookmarked
Posted 24 Nov 2001

Sorting HTML Tables using Javascript

, 24 Sep 2002
Rate this:
Please Sign up or sign in to vote.
A simple method of making your HTML tables sortable. No additional coding necessary!
<!-- Download Links -->

demo image <!-- Main HTML starts here -->


This JavaScript code can be used to convert tables in ordinary HTML into sortable ones by associating each title column with an onClick event to automatically sort the data rows.

No additional coding is necessary. All you need to do is give your table an ID field, include the sortTable.js file and call initTable in your document's onLoad method.

<TITLE>Sample Table</TITLE>
<BODY onLoad='initTable("table0");'>

<SCRIPT SRC="sortTable.js"></SCRIPT>

<TABLE ID="table0" BORDER=1>
	<TR><TD>ID</TD><TD>Device ID</TD></TR>
	<TR><TD COLSPAN=4>&nbsp;</TD></TR>
	<TR><TD COLSPAN=3>&nbsp;</TD><TD>&nbsp;</TD></TR>
	<TR><TD>1</TD><TD>k</TD><TD>00030</TD><TD>11/09/01 12:14:00 pm</TD></TR>
	<TR><TD>2</TD><TD>c</TD><TD>00006</TD><TD>11/11/01 12:15:00 pm</TD></TR>
	<TR><TD>3</TD><TD>a</TD><TD>00016</TD><TD>10/16/01 08:14:00 am</TD></TR>
	<TR><TD>4</TD><TD>b</TD><TD>00031</TD><TD>09/05/01 10:05:00 am</TD></TR>


27 Nov 2001 - updated source

3 Dec 2001 - updated source to include better comments. Also changed the main engine so that the data rows with various properties can be swapped as a whole. This change makes the script more flexible

6 Dec 2001 - The script now uses delete/insertCell for all the cell manipulation. So, what does this mean? Well, it's really flexible now. It should retain all the cell properties even when the script gets applied, including those of the title cells.

18 Dec 2001 - Fixed a few more bugs and improved the script so that it can handle different types of title cells.

23 Jan 2002 - Updated source files

15 Mar 2002

  • Automatic sort upon page load
  • Handling of <THEAD> <TFOOT> tags

5 Apr 2002

  • The script now preserves most common cell properties.
  • Bug fix in isDate method.

10 Apr 2002

  • Title rows can now be in a separate table
  • CSS preserved for cells
  • Bug fix in the status caption
  • Title link events now propagated to the entire cells

25 Apr 2002

  • Removes HTML tags when comparing cell content

25 July 2002:

  • Handles Euro date format
  • Fixed up/down replacement reg exp
  • Title column hints (via TITLE) added
  • Changed up/down char to up/down arrows
  • Up/down arrows get displayed if doSortUponLoad is set
  • Added a new feature to allow skipping columns when sorting
  • Title cells can be highlighted (bgcolor chaged) onMouseOver ...

25 Sep 2002:

  • Mouse pointer becomes hourglass while sorting
  • Bug fix where sorting is mistakenly done twice if doSortUponLoad is set
  • Sort direction alternates even between tables


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Han Yu
Web Developer
United States United States

You may also be interested in...


Comments and Discussions

Questionsorting in codeigniter Pin
Member 1287861930-Nov-16 0:51
memberMember 1287861930-Nov-16 0:51 
Questionlicense Pin
nikusoft12-Jun-15 3:53
membernikusoft12-Jun-15 3:53 
Questionkeep 1 column unchanged Pin
Member 1083389421-May-14 0:06
memberMember 1083389421-May-14 0:06 
QuestionNot works when there are more than 100000 rows Pin
Ashish payghan18-Feb-14 0:07
memberAshish payghan18-Feb-14 0:07 
QuestionCSS formatting Disappear Pin
Member 1040717723-Dec-13 0:01
memberMember 1040717723-Dec-13 0:01 
QuestionThank you! Pin
Member 1022844423-Aug-13 10:27
memberMember 1022844423-Aug-13 10:27 
Questiononmouseover onclick onmouseout..... is not sorting Pin
Member 951037916-Oct-12 3:48
memberMember 951037916-Oct-12 3:48 
QuestionGreat Code Pin
muthuraman a14-Aug-12 20:28
membermuthuraman a14-Aug-12 20:28 
QuestionGreat Work Dude Pin
RavikumarR1-Jul-12 19:14
memberRavikumarR1-Jul-12 19:14 
Questioninclude the <a tag in XSL file Pin
Member 915222021-Jun-12 4:36
memberMember 915222021-Jun-12 4:36 
QuestionSort by Ascending or Descending Pin
RachelWoulf2-Aug-10 9:50
memberRachelWoulf2-Aug-10 9:50 
AnswerRe: Sort by Ascending or Descending Pin
rwoulf2-Aug-10 12:02
memberrwoulf2-Aug-10 12:02 
GeneralFinding rowindex of the Html table in javascript Pin
Rajeshgut13-Oct-09 4:20
memberRajeshgut13-Oct-09 4:20 
GeneralSorting by second word in a column. Pin
John Rogers24-Jun-08 15:50
memberJohn Rogers24-Jun-08 15:50 
Questionhelp....? Pin
bakfull soulie6-May-08 10:18
memberbakfull soulie6-May-08 10:18 
AnswerRe: help....? Pin
John Rogers24-Jun-08 15:46
memberJohn Rogers24-Jun-08 15:46 
Questionsetting columns to ignore Pin
alastarwaterwolf21-Aug-07 13:13
memberalastarwaterwolf21-Aug-07 13:13 
Questionforums Pin
viji2703198520-Aug-07 4:25
memberviji2703198520-Aug-07 4:25 
Generalgreat script Pin
rodsavestuff14-Feb-07 9:01
memberrodsavestuff14-Feb-07 9:01 
QuestionProblems with columns with select fields Pin
steffersonj14-Nov-06 4:42
membersteffersonj14-Nov-06 4:42 
Generalfixed header Pin
J51219826-Aug-06 21:29
memberJ51219826-Aug-06 21:29 
GeneralRe-initialization in conjunction with Filter Pin
ahooja28-Jun-06 6:12
memberahooja28-Jun-06 6:12 
GeneralProblem with empty cells Pin
siribudigi9-May-06 7:44
membersiribudigi9-May-06 7:44 
QuestionHow to sort on columns having checkbox ? Pin
CottonGear20-Oct-05 1:07
memberCottonGear20-Oct-05 1:07 
GeneralDate with format DD-MON-YY is not sorting properly Pin
dhan_raj14-Oct-05 1:10
memberdhan_raj14-Oct-05 1:10 
GeneralTry this script, its awesome Pin
S.A.S.18-Sep-05 22:16
memberS.A.S.18-Sep-05 22:16 
GeneralChanging the column that sorts Pin
CrystalB2515-Feb-05 9:45
memberCrystalB2515-Feb-05 9:45 
GeneralRe: Changing the column that sorts Pin
androidik18-Mar-05 3:55
memberandroidik18-Mar-05 3:55 
Generalwrong css class in firefox Pin
Laubi_9-Jan-05 3:22
memberLaubi_9-Jan-05 3:22 
Generalskip rows while sorting table. Great Effort Pin
sreepavanic18-Nov-04 4:11
membersreepavanic18-Nov-04 4:11 
GeneralFormatting Table and fields Pin
gareth135168498121-Apr-04 1:09
membergareth135168498121-Apr-04 1:09 
GeneralCells with a Link Pin
JoeStone21-Jan-04 8:20
memberJoeStone21-Jan-04 8:20 
GeneralSorting a table on XML page Pin
Anonymous26-Nov-03 9:27
sussAnonymous26-Nov-03 9:27 
GeneralAnother bug found!!! Pin
Zhuofei Wang18-Sep-03 15:47
memberZhuofei Wang18-Sep-03 15:47 
GeneralEmpty cells problem Pin
DrJ210-Aug-03 21:35
memberDrJ210-Aug-03 21:35 
GeneralVery Slow for large data Pin
amitsinha4-Aug-03 9:54
memberamitsinha4-Aug-03 9:54 
GeneralRe: Very Slow for large data Pin
Stephane Rodriguez.4-Aug-03 10:25
memberStephane Rodriguez.4-Aug-03 10:25 
GeneralRe: Very Slow for large data Pin
Qing Jiang24-Feb-04 4:53
memberQing Jiang24-Feb-04 4:53 
GeneralRe: Very Slow for large data Pin
Stephane Rodriguez.24-Feb-04 5:28
memberStephane Rodriguez.24-Feb-04 5:28 
GeneralRe: Very Slow for large data Pin
_web_24-Jun-05 12:52
member_web_24-Jun-05 12:52 
GeneralCells with links in data Pin
Dan Hogan12-Jun-03 12:15
memberDan Hogan12-Jun-03 12:15 
GeneralRe: Cells with links in data Pin
Julia Belonojko8-Jul-03 8:54
sussJulia Belonojko8-Jul-03 8:54 
GeneralRe: Cells with links in data Pin
Dan Hogan8-Jul-03 9:03
memberDan Hogan8-Jul-03 9:03 
QuestionRe: Cells with links in data Pin
ken50423-Nov-05 20:42
memberken50423-Nov-05 20:42 
GeneralSeparate table for titles Pin
ekempter20-May-03 7:49
memberekempter20-May-03 7:49 
GeneralRow properties Pin
Alexander Blood3-Dec-02 11:20
memberAlexander Blood3-Dec-02 11:20 
GeneralRe: Row properties Pin
Han Yu3-Dec-02 14:26
memberHan Yu3-Dec-02 14:26 
GeneralRe: Row properties Pin
Alexander Blood4-Dec-02 10:48
memberAlexander Blood4-Dec-02 10:48 
GeneralRe: Row properties Pin
netclectic14-Nov-03 1:07
sussnetclectic14-Nov-03 1:07 
GeneralRe: Row properties Pin
Alexander Blood14-Nov-03 4:28
memberAlexander Blood14-Nov-03 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 25 Sep 2002
Article Copyright 2001 by Han Yu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid