Jump to content

Improved table sort


Jurjen Bos

Recommended Posts

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

Edited by Jurjen Bos
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...