简体   繁体   中英

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.

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM