The JavaScript table sort algorithm on the page "How TO - Sort a Table" is quite inefficient. Not only does it use the infamous quadratic "bubble sort", but is also updates the table unnecessarily often.
I was just learning JavaScript and HTML and was frustrated to find no reasonable example anywhere on the internet; so I made one myself 😁.
Please use the attached algorithm, which is basically the same, but with three improvements:
- Faster sorting by using binary insertion;
- updates the table only when needed;
- uses textContent instead of innerHTML so it is not distracted by invisible data.
Feel free to adjust the program to your style. If you are interested, I am happy to spice it up with some comments for legibility, but it is quite trivial anyway.
Jurjen
tablesort.js