Sortable Table Template

Empower users to organize data their way with this interactive, sortable table.

By clicking on a column header, users can sort the table rows in ascending or descending order, making it much easier to find and analyze information.

Screenshot of a table with up/down arrows in the column headers, indicating sort functionality.

Get Source Code → Preview →

About this Sortable Table

Static tables are useful, but dynamic, sortable tables provide a significantly better user experience for data analysis. This template provides the complete HTML, CSS, and JavaScript structure to create this functionality from scratch.

The core of this template lies in the JavaScript. When a header is clicked:

  1. It identifies the column index and the data type (text or numbers).
  2. It reads all the rows from the tbody.
  3. It performs a sort operation on the rows based on the content of the selected column.
  4. Finally, it re-appends the sorted rows back into the table.

The CSS adds a visual cue to the clickable headers (a "sort" icon) and indicates the current sort direction.

Key Features:

Implementation Notes:

This is a lightweight, educational example. For very large datasets (thousands of rows), complex data types, or advanced features like multi-column sorting, it's recommended to use a robust, dedicated library like DataTables.net.

Code

Here's the full code, including the HTML, CSS, and JavaScript required for the sortable table:

View Output