
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to sort an HTML table using JavaScript?
To sort an HTML table using JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <title>Sort a HTML Table Alphabetically</title> <style> button { padding: 10px; margin-bottom: 5px; font-size: 16px; font-weight: bold; } body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } table { border-spacing: 0; width: 100%; border: 1px solid #ddd; } th, td { border-top: 1px solid black; text-align: left; padding: 16px; } </style> </head> <body> <h1>Sorting table example</h1> <button onclick="sortTable()">Sort</button> <table class="filterTable"> <tr> <th>Name</th> <th>BirthDay Month</th> </tr> <tr> <td>Ron</td> <td>January</td> </tr> <tr> <td>Shawn</td> <td>April</td> <tr> <tr> <td>Caleb</td> <td>December</td> </tr> <tr> <td>Bruno</td> <td>February</td> </tr> <tr> <td>Jack</td> <td>October</td> </tr> <tr> <td>Max</td> <td>November</td> </tr> </table> <script> function sortTable() { var filterTable, rows, sorted, i, x, y, sortFlag; filterTable = document.querySelector(".filterTable"); sorted = true; while (sorted) { sorted = false; rows = filterTable.rows; for (i = 1; i < rows.length - 1; i++) { sortFlag = false; x = rows[i].getElementsByTagName("TD")[0]; y = rows[i + 1].getElementsByTagName("TD")[0]; if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { sortFlag = true; break; } } if (sortFlag) { rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); sorted = true; } } } </script> </body> </html>
Output
The above code will produce the following output −
On clicking the Sort button −
- Related Articles
- How to sort an HTML list using JavaScript?
- How to sort rows in a table using JavaScript?
- Using merge sort to recursive sort an array JavaScript
- How to submit an HTML form using JavaScript?
- How to convert JSON data to a html table using JavaScript/jQuery?
- How to use an HTML tag inside HTML table?
- JavaScript Get row count of an HTML table?
- How to get text from each cell of an HTML table using Selenium?
- How to Create an Analog Clock using HTML, CSS, and JavaScript?
- How to Create an Image Slider using HTML, CSS, and JavaScript?
- How to upload an image using HTML and JavaScript in Firebase?
- How to add an element horizontally in HTML page using JavaScript?
- Apply an IF condition to every element in an HTML table with JavaScript?
- How to return HTML or build HTML using JavaScript?
- How to center the contents of an HTML table?

Advertisements