Wrox Home  
Search P2P Archive for: Go

  Return to Index  

interdev_programming thread: problem with Java Script


Message #1 by David Seto <dctseto@i...> on Sun, 18 Mar 2001 11:26:30 -0800
Hi Matthew,

Sorry for the mix-up. What you had mentioned are not the mistakes in the 
original code.  The program looks like it is working. But please look 
carefully at the output from the browser. str1 is not sorted correctly 
when printed the second time. (Although I did nothing to str1.)

I guess I did not copy the Java Script correctly into my email last 
time. I just copied it from Visual Interdev. The following is the 
original code:


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>JavaScript sort numbers</TITLE>
    <script language="javascript">
    var str1, str2;
    function numsort(var1, var2) {
    return var1 - var2;
    }
    array1 = new Array("1", "2", "12", "34", "72");
    str1 = array1.sort(numsort);
    document.write("str1 is numerically sorted as: " + str1 + "<br>");
    str2 = array1.sort();
    document.write("str2 is alphabetically sorted as: " + str2 + "<br>");
    document.write("---------------------" + "<br>");
    document.write("Reprint again" + "<br>" + "<br>");
    document.write("str1 is numerically sorted as: " + str1 + "<br>");
    document.write("str2 is alphabetically sorted as: " + str2);
    </script>
</HEAD>
<BODY>

<P> </P>

</BODY>
</HTML>

The output is as Follows:

str1 is numerically sorted as: 1,2,12,34,72
str2 is alphabetically sorted as: 1,12,2,34,72
---------------------
Reprint again

str1 is numerically sorted as: 1,12,2,34,72
str2 is alphabetically sorted as: 1,12,2,34,72


Evidently, str1 is not sorted numerically any more in the second 
printing. I didn't touch str1 at all after sorting. Why would this 
variable change its value ? I had been scratching my head for this 
behavior. Do appreciate very much if someone can tell me why.

Thanks.

David Seto


  Return to Index