Sorting table with jQuery how to

user7376146

I know there are a lot of plugins for table sorting, but I would like to learn it myself. So here is my table.

<table class="table-bordered sortable">
    <tr>
      <th>Name</th>
      <th>Wage</th>
      <th>Age</th>
      <th>City</th>
    </tr>
    <tr>
      <td>Jack Carver</td>
      <td>368</td>
      <td>45</td>
      <td>New York</td>
    </tr>
    <tr>
      <td>John Johnson</td>
      <td>450</td>
      <td>19</td>
      <td>Atlanta</td>
    </tr>
    <tr>
      <td>Steve McKenzie</td>
      <td>891</td>
      <td>88</td>
      <td>Miami</td>
    </tr>
  </table>

How can I make my table sortable? If user clicks on th, relevant tds should be sort alphabetically (and rest of row as well).

Richard Ye

Here is a general approach. No one step is particularly difficult, it's just sometimes hard to put everything together:

  1. Bind an event handler to each of the <th> elements
  2. This event handler will need to look at all of the <tr> elements, and put those into an array.
  3. Sort the <tr> elements with Array.sort(). The sorting function will need to look at the contents of <td> child element of the <tr> passed in with the same index as the <th> element clicked.
  4. Clear the <tbody> and reinsert the <tr>s in the sorted order.
  5. Add some sort of indicator to the <th> to indicate that it is sorted.

Now I wouldn't use the result of this technique in production because it is quite fragile and we shouldn't be manipulating DOM directly in this fashion, but at least it is a good exercise to develop JS skills.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

sorting <table> with JavaScript (jQuery)

sorting html table with jquery

Jquery table sorting Alphabetically, not sorting numerically

convert HTML table to JQuery table for sorting

How to skip table sorting in phpmyadmin

Table sorting as per last column Jquery

Jquery UI sorting multiple table row at once

jQuery multi-level table sorting

Jquery Data Table Filter Custom Sorting onload?

JQuery Table Sorting - More Efficient way?

Strange behavior on jQuery append() when sorting table

Issue with table sorting using JavaScript and jQuery

How to sort table in ascending order first on change of sorting column using tablesorter in jquery?

Sorting Jquery Table that JSON data appended via jQuery

How to avoid sorting pivot table by index

How to set a default sorting in a react table?

how to trigger on span element when sorting table

How to add sorting to table with react virtualized?

How to do default sorting in react-table

How to disable sorting in Movilizer table Columns

How to apply sorting in Material table with conditional checkboxes

How to set a default sorting to a jh-table?

How to change the sorting order in Antd's table?

How to enable specific sorting on ng zorro table?

How to make jQuery sorting work with more characters?

jQuery dataTables: sorting a table with custom filter applied causes rows disappear

sorting elements that aren't in a HTML table using jquery

the sorting of Gridview using jQuery Table Sorter of asp.net webfrom

Issue with sorting date columns with the jQuery table-sorter plugin