Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to optimise my script ????

Status
Not open for further replies.

moretips

MIS
Sep 25, 2000
25
US
Hi !!! Below is a script which used to sort the data in a table. When u click one of the button, the other data in other column will be sorted. My problem now is the script is too big, i need to reduced the size. I think the most heaviest part is the exchange function which use bubble sort. Can any one give commend to my script in order to reduced the size ???? Thanks !!!!

U may visit this url -->to have a look .

<html>
<head>
<title>Java Script</title>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function dates(da,db){
return (((d2(da)-d2(db)) < 0) ? 0:1);
}

function d2(d){
return ((d.substring(0,4)*1000)+(d.substring(5,7)*100) +(d.substring(8,10)*1));
}

function exchange(i){
var f = document.forms;
var a = f.check.value;
f.check.value = f[i+1].check.value;
f[i+1].check.value = a;
a = f.units.value;
f.units.value = f[i+1].units.value;
f[i+1].units.value = a;
a = f.amount.value;
f.amount.value = f[i+1].amount.value;
f[i+1].amount.value = a;
a = f.presented.value;
f.presented.value = f[i+1].presented.value;
f[i+1].presented.value = a;
}

function Sort(num)
{
/* orders the table by amount,check number,date,presented */
for (var j = 0; j < 5; j++)
{
for(var i = 0; i < 5; i++)
{
var a = document.forms, b = document.forms[i+1];
if ( ( ( num == 3 ) && ( 1 * a.amount.value > 1 * b.amount.value ) ) ||
( ( num == 2 ) && ( 1 * a.check.value > 1 * b.check.value ) ) ||
( ( num == 1 ) && ( dates ( a.units.value, b.units.value ) ) ) ||
( ( num == 4 ) && ( a.presented.value > b.presented.value ) ) )
{exchange (i);}
}
}
}

</SCRIPT>
</HEAD>
<body>
<center>
<table border=4>
<form method=post>
<tr>
<td><div align=center> <input type=&quot;button&quot; value=&quot;DATE&quot; onclick=Sort(1)> </div></td>
<td><div align=center> <input type=&quot;button&quot; value=&quot;CK #&quot; onClick=Sort(2)> </div></td>
<td><div align=center> <input type=&quot;button&quot; value=&quot;AMOUNT&quot; onClick=Sort(3)> </div></td>
<td><div align=center> <input type=&quot;button&quot; value=&quot;PAID TO&quot; onClick=Sort(4)> </div></td>
</tr>
<tr>
<td><input type=text name=units></td>
<td><input type=text name=check></td>
<td><input type=text name=amount></td>
<td><input type=text name=presented></td>
</tr>
</form>
<form method=post>
<tr>
<td><input type=text name=units></td>
<td><input type=text name=check></td>
<td><input type=text name=amount></td>
<td><input type=text name=presented></td>
</tr>
</form>
<form method=post>
<tr>
<td><input type=text name=units></td>
<td><input type=text name=check></td>
<td><input type=text name=amount></td>
<td><input type=text name=presented></td>
</tr>
</form>
<form method=post>
<tr>
<td><input type=text name=units></td>
<td><input type=text name=check></td>
<td><input type=text name=amount></td>
<td><input type=text name=presented></td>
</tr>
</form>
<form method=post>
<tr>
<td><input type=text name=units></td>
<td><input type=text name=check></td>
<td><input type=text name=amount></td>
<td><input type=text name=presented></td>
</tr>
</form>
<form method=post>
<tr>
<td><input type=text name=units> </td>
<td><input type=text name=check></td>
<td><input type=text name=amount></td>
<td><input type=text name=presented></td>
</tr>
</form>
</table>
</center>
</body>
</html> [sig][/sig]
 
What do you mean its too big? It looks like a nice piece of coding to me. If you mean the file is too big then you could easily come up with a way to create the forms dynamically with js rather than statically with html. This would reduce the size of the file dramatically. If you mean the code has become too large I can't say that I agree as 24 lines of js to do a multi &quot;array&quot; sort seems pretty darn good. There are some other methods for sorting that you could use (examples at: that may be quicker on large sorts. There is a lot of good theory information on the internet.

Good luck,

Rob
&quot;Focus on the solution to the problem, not the obstacles in the way.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top