1

Question

Given the following HTML table

<table id="sort-table">
    <tbody>
        <tr>
            <td>text*1234</td>
            <td>data</td>
        </tr>
        <tr>
            <td>text*1224</td>
            <td>data</td>
        </tr>
        <tr>
            <td>text*1254</td>
            <td>data</td>
        </tr>
        <tr>
            <td>texta*1234</td>
            <td>data</td>
        </tr>
        <tr>
            <td>textabc*1234</td>
            <td>data</td>
        </tr>
        <tr>
            <td>textab*1234</td>
            <td>data</td>
        </tr>
        <tr>
            <td>*2234</td>
            <td>data</td>
        </tr>
        <tr>
            <td>textabcd*1234</td>
            <td>data</td>
        </tr>
        <tr>
            <td>text*1234</td>
            <td>data</td>
        </tr>
        <tr>
            <td>textabc*1234*1234</td>
            <td>data</td>
        </tr>
    </tbody>
</table>

Is there a way to sort the rows inside of the 'sort-table' table by the value of the first <td> element in each <tr> element? Sorting should be Alphabetical by each 'text' first, and then numeric by the value after the *.

JQuery is available, but answers composed of vanilla Javascript will be preferred.

Attempted Solution

Using the following Javascript I have attempted to achieve the elusive functionality.

//get the rows of the table and put them in a NodeList
var tableRows = $("#sort-table")[0].children[0].children;
//create an empty 'tbody' element to replace the old table contents with
var tBody = document.createElement("tbody");
//get the old tbody element to replace it with an empty tbody element
var oldBody = $("#sort-table")[0].children[0];
//get the NodeList as an array
var rowsArray = [].slice.call(tableRows, 1);

//sort the array
rowsArray.sort();

//replace the current table body with an empty one
oldBody.parentNode.replaceChild(tBody, oldBody);

//put each array element in the new table body
rowsArray.forEach(function(entry) {
    var tableRef = document.getElementById('sort-table').getElementsByTagName('tbody')[0];

    // Insert a row in the table at the last row
    var newRow   = tableRef.insertRow(tableRef.rows.length);

    // Insert a cell in the row at index 0
    var newCell  = newRow.insertCell(0);

    newCell.appendChild(entry);
});

here is a jsfiddle showing how this currently works

1 Answer 1

3

You can use the sort() method to organise the rows in to the order required:

$('#sort-table tr').sort(function(a, b) {
    var aText = $(a).find('td:eq(0)').text();
    var bText = $(b).find('td:eq(0)').text();

    if (aText > bText)
        return 1;
    else if (aText < bText)
        return -1;
    return 0;
}).appendTo('#sort-table');

Updated example

Sign up to request clarification or add additional context in comments.

5 Comments

The sort function is kind of wrong, see this: stackoverflow.com/questions/31749051/sorting-divs-using-jquery/…
@MinusFour so the .sort() method may do some weird things with out-of-date browsers, but modern browsers are fine right? To be sure, nothing appears to be incorrect in the behavior of the input camparison function.
@LanguidSquid no, it's about sort using 3 different values (1, 0, -1) to sort out the array. When you do return a > b; you either return true or false, 1 or 0. Note: It's not really 1 and -1. Positive and Negative is more accurate.
@MinusFour I believe I see what you are saying now. The Comparison function passed in returns a (true or false) value as apposed to a (1, 0 or -1) value. This could cause some wonky behavior? While it works for the test data I have provided, maybe it requires some doctoring before being set free in the wilderness.
@MinusFour You're right that this implementation of the sort() isn't strictly best practice - although it will work fine due to Javascript's loose data types. I'll amend it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.