Tag Archive for: sort

Sort table rows by clicking on table header column

Sort an array of numeric, alphabetic and alphanumeric values

Thanks to japanfever who basically reached this one line approach!

Rename when destructuring

Sort an array of objects

Sort an array of objects by their properties values.

Sort by table content

table

How can you sort any HTML table using jQuery?

Element 40
Element 2
Element 29
Element 28
Element 5

Suppose you only want to use the number.

  1. First we must sort elements
  2. After that we have to keep going deleting each element and appending again to the table with the new order
// JS
$("table tr").sort(function (a, b) {
    var a_num = $(a).children(":first-child").text().split(" ")[1];
    var b_num = $(b).children(":first-child").text().split(" ")[1];
    return parseInt(a_num) - parseInt(b_num);
}).each(function(){
    var elem = $(this);
    elem.remove();
    $(elem).appendTo("table > tbody");
});

See a real example:

Edited: improved and without jQuery.