Sorting tables dynamically with javascript is often a smooth solution to your sorting problems. I’m here going to put a simple solution that I have been using for a while. I have in no means created this solution – I found it somewhere on the web and now I just want to put it here for easy access.
The table needs to have the following structure for this solution to work:
column1 | column2 | column3 |
---|---|---|
value1 | value2 | value3 |
value4 | value5 | value6 |
value7 | value8 | value9 |
The critical parts here is that the table needs an id and a tbody. Without those the sorting will not work
Here is the JQuery code (standard JQuery, no plugins needed)
function sortTable(){ var rows = $('#mytable tbody tr').get(); rows.sort(function(a, b) { var A = $(a).children('td').eq(1).text().toUpperCase(); var B = $(b).children('td').eq(1).text().toUpperCase(); if(A < B) { return -1; } if(A > B) { return 1; } return 0; }); $.each(rows, function(index, row) { $('#mytable').children('tbody').append(row); }); }
To sort according to the second column we use ‘.eg(1)’ in row 6 and 7. Second column ‘.eg(3)’ and so on
Here is a JSFiddle of the solution: JQuery sort table rows on column value
Tested on Chrome v44.0.2403.155 (64-bit) and JQuery 1.9.1
This is nice. I made some slight mods so I could sort by direction and have alternate row colors.
Nice. Thank you. I’ll post it so it might benefit someone else too
ya its working now
Thanks for the share. Helped and Saved my time.
Thanks so much, that saved me a lot of time!
thank you very much, this helped me a lot (and I could avoid a postback in asp.net)
I did some extra things, to enable sorting for more than one row and alternating sort order
I can now sort several columns and the 2nd click on the same colum orders desc instead of asc. Empty rows come last.
persist… and retreive access a backing hidden fild to survive asp.net postbacks
Many thanks for sharing this!
I have a table with 8 rows that I sort on column one(1). When I run the code it sorts that data correctly but i end up with the data duplicate(and sorted) 8 times. What am I missing? How do I prevent the data being duplicated and sorted 8 times?
Thanks.
I’m not sure what your problem can be but you can play around with my solution here: http://jsfiddle.net/malen/6sskjbod/ Maybe you get lucky and find out what’s wrong 🙂
what returns 1 or -1 do in this case
If -1, A is less than B and if 1 B is less than A. It sorts according to alphanumeric order, based on the TD element text
Great Code
Thanks
😛
You do not require an id.
var table = that.closest(‘table’);
var tbody = table.find(‘tbody’);
var rows = tbody.find(‘tr’).get();
Yep, that is correct. I usually use an id on all my function tags so that did not occur to me 🙂
I would update line #2 to the following to handle nested tables. If not it will attempt to sort the nested table rows.
var rows = $(‘#livefeeds>tbody>tr’).get();
In addition if you are attempting to sort a date, you will need to cast it as a date value or sort the text by unix time or a YYYY/MM/DD format.
Good stuff – thanks for sharing!
Very nice. Small and effective. Thumbs up.
from this line
var rows = $(‘#mytable tbody tr’).get();
i was getting blank array. please let me know what happen
Hi Richa,
Does your table have id=”mytable”? If not, you need to change the “#mytable” to whatever your table is called
I’m pretty new to jQuery. The code you posted works perfectly but I’m having trouble understanding something. I don’t see anything in the code that explicitly clears the content of before sorting the array of rows. Why doesn’t the append() call applied to each row result in a duplicate of the row being added to the end of ?
If the object being appended by append() is already part of the target’s content then is it moved from its current location to the end instead of adding a new copy?
Thanks in advance!
Hi there, Yes you assumption is correct. You can read more about the append() functino in JQuery here: http://api.jquery.com/append/