Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    44
    Thanks
    11
    Thanked 0 Times in 0 Posts

    sort() & table cells

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    window.onload = function(){
    		document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    	}
    
    var names = ["Marble","Jarque","Dino","Pineapple"];
    
    function abc(){
    	if(document.getElementById('myTable').rows[0].cells[0].innerHTML==names[0]){names.sort()}
    	else if(document.getElementById('myTable').rows[0].cells[0].innerHTML==names[2]){names.sort().reverse()}
    	else {names.sort()}
    	document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    
    	}
    
    </script>
    </head>
    <body>
    <div id="t"></div>
    <input type="button" value="sort" onclick="abc()" />
    </body>
    </html>
    First time sort: no problem
    But when it comes to the blue-highlighted parts
    It cannot do the sort().reserve()
    What's wrong with the codes?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    After the sort Dino becomes rows[0].cells[0].

    Code:
    var names = ["Marble","Jarque","Dino","Pineapple"];
    
    function abc(){
    if(document.getElementById('myTable').rows[0].cells[0].innerHTML==names[0]){names.sort();alert (names[2])}  // Marble
    document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    alert (document.getElementById('myTable').rows[0].cells[0].innerHTML)  // Dino
    if(document.getElementById('myTable').rows[0].cells[0].innerHTML==names[0]){names.sort().reverse()}
    else {names.sort()}
    document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    }
    Last edited by Philip M; 08-27-2011 at 12:21 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    44
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    After the sort Dino becomes rows[0].cells[0].

    Code:
    var names = ["Marble","Jarque","Dino","Pineapple"];
    
    function abc(){
    if(document.getElementById('myTable').rows[0].cells[0].innerHTML==names[0]){names.sort();alert (names[2])}  // Marble
    document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    alert (document.getElementById('myTable').rows[0].cells[0].innerHTML)  // Dino
    if(document.getElementById('myTable').rows[0].cells[0].innerHTML==names[0]){names.sort().reverse()}
    else {names.sort()}
    document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    }
    Code:
    function abc(){
    	if(document.getElementById('myTable').rows[0].cells[0].innerHTML==names[0]){names.sort()}
    	else if(document.getElementById('myTable').rows[0].cells[0].innerHTML=="Dino"){names.sort().reverse()}
    	else {names.sort()}
    	document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    }
    But why even I change it, it cannot do the sort().reverse()?

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    49
    Thanks
    0
    Thanked 13 Times in 12 Posts
    When you run the sort method on a array, it changes the original array. Since you might want to use this function on different arrays, it isn't very efficient to test for the value of the first row. So when you want to toggle between ascending and descending it's best to keep the state in a variable.
    Code:
    function abc(){
     abc.sorted == null ? names.sort() : names.reverse();
     abc.sorted = true;
     document.getElementById("t").innerHTML = "<table border='1' id='myTable'>" + "<tr><td>" + names.join("<tr><td>") + "</td></tr>" + "</table>";
    };

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    44
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks. I learn one more thing about the tenary operator and it's possible an array order can be changed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •