user1166981
user1166981

Reputation: 1746

How to sort a a table with Javascript without library?

There is two main aims:

  1. Sort table of data using javascript without libraries such as jquery, so pure javascript.

  2. Sort/group rows together based on class/id (like css).

I thought maybe getElementById could be useful, but don't know enough javascript to investigate properly.

Upvotes: 0

Views: 395

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

A table's rows can be found in the rows[] array of said table.

You can create an array containing those rows.

You can then .sort() the array based on a callback function that looks up certain info, in the form
.sort(function(a,b) { /* return -1 if a comes before b or 1 if b comes before a */ })

If you iterate over the array and appendChild the rows to the table they are in, the result is a sorted table.

If you have problems with the actual code, please post what you have so far and we can help more specifically ;)

Upvotes: 1

Related Questions