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.

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:
- It identifies the column index and the data type (text or numbers).
- It reads all the rows from the
tbody
. - It performs a sort operation on the rows based on the content of the selected column.
- 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:
- Client-Side Sorting: Instantly sorts data in the browser without needing to reload the page.
- Sorts Text and Numbers: The included JavaScript can handle both alphabetical and numerical sorting.
- Visual Indicators: CSS adds icons to the headers to show which column is being sorted and in which direction.
- Dependency-Free: The entire solution uses "vanilla" JavaScript, meaning no external libraries like jQuery are required.
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: