The Table Sort code snippet will automatically append ↑ (up) and ↓ (down) arrows next to <th> text in the <thead> of any table with the class name table-sort. Clicking on the up arrow will sort the rows alphanumerically by that column's values. Clicking on the down arrow will, in contrast, sort the rows in reverse alphanumeric order.

If the <th> has the class name table-sort-default, then it is assumed the table is already sorted by that column. The default arrow for that column will then be ↓ (down) to reverse sort.


This is an example table with the class name table-sort. The Numerical <th> has the class name table-sort-default.

One 1 Uno
Two 2 Dos
Three 3 Tres
Four 4 Cuatro
Five 5 Cinco
Six 6 Seis
Seven 7 Siete
Eight 8 Ocho
Nine 9 Nueve
Ten 10 Diez
<table class="bordered table-sort">
			<th class="table-sort-default">Numerical</th>
		<tr> <td>One</td>   <td>1</td>  <td>Uno</td>    </tr>
		<tr> <td>Two</td>   <td>2</td>  <td>Dos</td>    </tr>
		<tr> <td>Three</td> <td>3</td>  <td>Tres</td>   </tr>
		<tr> <td>Four</td>  <td>4</td>  <td>Cuatro</td> </tr>
		<tr> <td>Five</td>  <td>5</td>  <td>Cinco</td>  </tr>
		<tr> <td>Six</td>   <td>6</td>  <td>Seis</td>   </tr>
		<tr> <td>Seven</td> <td>7</td>  <td>Siete</td>  </tr>
		<tr> <td>Eight</td> <td>8</td>  <td>Ocho</td>   </tr>
		<tr> <td>Nine</td>  <td>9</td>  <td>Nueve</td>  </tr>
		<tr> <td>Ten</td>   <td>10</td> <td>Diez</td>   </tr>


To use this script, you must append the table-sort class names to any <table>s to be sortable. You may optionally give any <th>s the table-sort-default class. Once you have one or more tables with the aforementioned class name, add the following script to the end of your document, just before </body>:

<script src="//www.charlesstover.com/table-sort.js" type="text/javascript"></script>