0

Hi I have html structure with table. I want to sort td according to their value. I trying it but cant find the logic to make it happen. My function is

<head>
<script type="text/javascript">
function sorting(){
    var sortvalue= document.getElementsByTagName('td');
    for(i=0; i<sortvalue.length;i++){
        var val= sortvalue[i].value
        }
    }


</script>

</head>

<body>

<table width="500" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>5</td>
  </tr>
  <tr>
    <td>3</td>
  </tr>
  <tr>
    <td>2</td>
  </tr>
  <tr>
    <td>4</td>
  </tr>
    <tr>
    <td>1</td>
  </tr>


</table>

<a href="#" onclick="sorting()">click to sort</a>
</body>
2
  • if you're using a js library, there most definitely are plugins that will sort tables for you. Commented Sep 13, 2012 at 6:22
  • I wrote a reply awhile ago which might include useful links for such "dynamic" tables with sorting and stuff. Commented Sep 13, 2012 at 6:26

6 Answers 6

1

If you plan to do more than just organize those numbers: those saying you should use a plugin are correct. It'd take more effort than it's worth to try to make your own table sorter.

If all you want to do is sort those numbers (small to large):

function sorting() {
    td = document.getElementsByTagName("td");
    sorted = [];
    for (x = 0; x < td.length; x++)
        sorted[x] = Number(td[x].innerHTML);
    sorted.sort();
    for (x = 0; x < sorted.length; x++) 
        td[x].innerHTML = sorted[x];
}

Largest to smallest:

function sorting() {
    td = document.getElementsByTagName("td");
    sorted = [];
    for (x = 0; x < td.length; x++)
        sorted[x] = Number(td[x].innerHTML);
    sorted.sort().reverse();
    for (x = 0; x < sorted.length; x++) 
        td[x].innerHTML = sorted[x];
}
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that you're putting the script under your link, or adding it on domready:

function sorting(){

    var tbl = document.getElementsByTagName('table')[0];
    var store = [];
    for(var i=0, len=tbl.rows.length;i<len; i++){
        var row = tbl.rows[i];
        var sortnr = parseFloat(row.cells[0].textContent || row.cells[0].innerText);
        if(!isNaN(sortnr)) store.push([sortnr, row]);
    }
    store.sort(function(x,y){
        return x[0] - y[0];
    });
    for(var i=0, len=store.length; i<len; i++){
        tbl.appendChild(store[i][1]);
    }
    store = null;
}

link here: http://jsfiddle.net/UMjDb/

Comments

1

For your example you can make an array of the cell data from the node list and sort that array, and then replace the cells data with the sorted data. Simpler than moving elements.

<head>
<script type= "text/javascript">
function sorting(){
    var T= [], tds= document.getElementsByTagName('td');
    for(var i= 0;i<tds.length;i++){
        T.push(tds[i].firstChild.data);
    }
    T.sort(function(a, b){
        return a-b
    });
    for(var i= 0;i<tds.length;i++){
        tds[i].replaceChild(document.createTextNode(T[i]), tds[i].firstChild);
    }
}
</script>
</head>
<body>
<table width= "500" border= "0" cellspacing= "0" cellpadding= "0">
<tr>
<td>5</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr>
<td>1</td>
</tr>
</table>
<a href= "#" onclick= "sorting()">click to sort</a>
</body>

Comments

1

i giving a jquery solution, hope this post helps you.

var tdData = Array();
$(document).ready(function(){
    $('td').each(function(i){
   tdData [i] = $(this).text();
   });  
});

function sorting(){
   var sortedData = tdData.sort();
    $('td').each(function(i){
  $(this).text(sortedData[i]);
  } ); 
}

complete solution: link

Comments

1

step 1: find all td's
step 2: fetch their values
step 3: sort them on their values
step 4: put them back into their parent in the correct order. This can simply be done with an $(parent).html("").append(sortedNodes) (if you use jQuery that is).

As @FelixKling points out below, the .html("") is not strictly necessary other than for code clarity since "If you have nodes that already exist in the tree, then .append will remove them first from their current location and add them to the new parent"

3 Comments

(a) $(parent).html("").append(sortedNodes) is jQuery (which is not mentioned here) (b) $(parent).append(sortedNodes) suffices ;)
(a) true, included that in the answer. (b) not true, since we want to set the sortedNodes instead of the current nodes, and not in addition to the current nodes :)
If you have nodes that already exist in the tree, then .append will remove them first from their current location and add them to the new parent.
0

You need to re-organise the table. The simplest approach would be to use a plugin like this one for jQuery

You need to modify the DOM, they would be different way to do that, like grabbing all the data, removing all the rows and adding them back in the right order.
It could be improved better using detach and reattaching.

Comments

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.