What I'm after is to be able to change column sorted on the fly.
Main Topics
Browse All TopicsI have a page that I'm trying to let the user sort the information. It half works. I can't seem to figure out the other half. But that's the fun of it. Anyhow, I was hoping someone out there could look at my code and offer a suggestion or two.
The array is built during a pull from a db using SQL server. That part runs alright. The pull takes 2-5 mins so I was hoping to be able to sort the info on the client side.
Below is a simple copy of the page I'm working with.
<%@ LANGUAGE="VBSCRIPT" %>
<html>
<head>
<title>Test Search</title>
</head>
<body>
<SCRIPT Language="JavaScript">
// sort varibles
var lastSortBy = -1;
var sortGT = 1; // sort forwards
var sortLT = -1; // reverse sort
var sortStyle = new Array(" sorted <i>in reverse</i> by "," **impossible** "," sorted by ");
function fnOTSRecord( id, wo, pon, fnp, DS1_SYSID, City_Pair, WOSec)
{
this.id = id;
this.WorkOrder = wo;
this.pon = pon;
this.fnp = fnp;
this.DS1_SYSID = DS1_SYSID;
this.City_Pair = City_Pair;
this.WOSection = WOSec;
//alert("Constructing OTSRecord:\n" + id + "\n" + wo + "\n" + pon + "\n" + fnp);
}
var aOTSRecords = new Array(
new fnOTSRecord('2425','6817',
new fnOTSRecord('2425','6817',
new fnOTSRecord('6844','9587',
new fnOTSRecord('6845','009587
new fnOTSRecord('14226','13393
new fnOTSRecord('14271','01342
new fnOTSRecord('33943','22637
new fnOTSRecord('33944','22638
new fnOTSRecord('33945','22639
new fnOTSRecord('33946','22640
new fnOTSRecord('34804','23145
new fnOTSRecord('39756','25750
new fnOTSRecord('39757','02575
new fnOTSRecord('41028','02641
new fnOTSRecord('41028','02641
new fnOTSRecord('41561','26689
new fnOTSRecord('41561','26689
new fnOTSRecord('41643','26738
new fnOTSRecord('41644','02673
new fnOTSRecord('41644','02673
new fnOTSRecord('41645','02673
new fnOTSRecord('63394','40528
new fnOTSRecord('67496','43394
);
// functions to sort
function fnSortByID( arg1, arg2 )
{
if( arg1.ID == arg2.ID ) return 0;
return ( parseInt(arg1.ID) < parseInt(arg2.ID) ) ? sortLT : sortGT;
}
function fnSortByWorkOrder( arg1, arg2 )
{
if( arg1.WorkOrder == arg2.WorkOrder ) return 0;
return ( parseInt(arg1.WorkOrder) < parseInt(arg2.WorkOrder) ) ? sortLT : sortGT;
}
function fnSortByPon( arg1, arg2 )
{
if( arg1.pon == arg2.pon ) return 0;
return ( arg1.pon < arg2.pon ) ? sortLT : sortGT;
}
function fnSortByFnp( arg1, arg2 )
{
if( arg1.fnp == arg2.fnp ) return 0;
return ( parseInt(arg1.fnp) < parseInt(arg2.fnp) ) ? sortLT : sortGT;
}
function fnSortByDS1( arg1, arg2 )
{
if( arg1.DS1_SYSID == arg2.DS1_SYSID ) return 0;
return ( arg1.DS1_SYSID < arg2.DS1_SYSID ) ? sortLT : sortGT;
}
function fnSortByCity_Pair( arg1, arg2 )
{
if( arg1.City_Pair == arg2.City_Pair ) return 0;
return ( arg1.City_Pair < arg2.City_Pair ) ? sortLT : sortGT;
}
function fnSortByWOSection( arg1, arg2 )
{
if( arg1.WOSection == arg2.WOSection ) return 0;
return ( arg1.WOSection < arg2.WOSection ) ? sortLT : sortGT;
}
// Array of sort functions
aSortFuncs = new Array( fnSortByID, fnSortByWorkOrder, fnSortByPon, fnSortByFnp,
fnSortByDS1, fnSortByCity_Pair, fnSortByWOSection);
// Array of Field Names
aSortByNames = new Array( "ID", "Work Order", "PON", "FNP", "DS1 SYSID",
"City Pair", "WO-Section" );
//alert("sortGT & sortLT:\n" + sortGT + "\n" + sortLT);
function sortBy( fldNum )
{
alert("sortGT & sortLT:\n" + sortGT + "\n" + sortLT + "\n" + fldNum + "\n" + lastSortBy );
if ( fldNum != lastSortBy )
{
sortGT = 1;
sortLT = -1;
lastSortBy = fldNum;
} else {
sortGT = - sortGT;
sortLT = - sortLT; // reverse the sort
}
// alert("sortGT & sortLT:\n" + sortGT + "\n" + sortLT + "\n" + fldNum + "\n" + lastSortBy + "\n" + parseInt(curItem.id) );
aOTSRecords.sort( aSortFuncs[fldNum] ); //sort
// displaying results
//parent.Display.document
document.open( ); // clears frame and prepares for (re)write...
document.write( "<BODY bgColor=yellow>\n<CENTER>\
+ sortStyle[sortGT+1] + aSortByNames[fldNum] + "</H3><P>\n" );
document.write( "<!-- <FONT Size='-1'><I>Click on a column title to sort by that column; click again to reverse the sort.</I></FONT>\n -->");
document.write( "<TABLE Width='100%' Border=0 CellPadding=3 bgColor=lightgreen>\n<TR>\
for( col = 0; col < aSortByNames.length; ++col )
{
document.write( " <TH><A HREF='javascript:sortBy(" + col
+ ")'>" + aSortByNames[col] + "</A></TH>\n" );
}
document.write( "</TR>\n" );
for( row = 0; row < aOTSRecords.length; ++row )
{
curItem = aOTSRecords[row];
document.write( "<TR>\n" );
document.write( " <TD>" + parseInt(curItem.id) + "</TD>\n" );
document.write( " <TD>" + curItem.WorkOrder + "</TD>\n" );
document.write( " <TD>" + curItem.pon + "</TD>\n" );
document.write( " <TD>" + curItem.fnp + "</TD>\n" );
document.write( " <TD>" + curItem.DS1_SYSID + "</TD>\n" );
document.write( " <TD>" + curItem.City_Pair + "</TD>\n" );
document.write( " <TD>" + curItem.WOSection + "</TD>\n" );
document.write( "</TR>\n" );
}
document.write( "</TABLE>\n<P><HR>" );
document.write(aSortFuncs[
document.write (aOTSRecords[5].id + "</body>\n");
document.close( );
alert("sortGT & sortLT:\n" + sortGT + "\n" + sortLT + "\n" + fldNum + "\n" + lastSortBy + "\n" + parseInt(curItem.id) );
}
sortBy(0);
</SCRIPT>
</body>
</html>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Instead of writing the whole body, write only in a div tag or an iframe or something of that sort. Here is how you can do it using a div
<html>
<head>
<title>Test Search</title>
<SCRIPT Language="JavaScript">
function writeLayer(DivId,txt)
{
if(document.getElementById
{
document.getElementById(Di
}
else if(document.all)
{
document.all[DivId].innerH
}
else if(document.layers)
{
with(document.layers[DivId
{
open();
write(txt);
close();
}
}
}
</script>
<SCRIPT Language="JavaScript">
// sort varibles
var lastSortBy = -1;
var sortGT = 1; // sort forwards
var sortLT = -1; // reverse sort
var sortStyle = new Array(" sorted <i>in reverse</i> by "," **impossible** "," sorted by ");
function fnOTSRecord( id, wo, pon, fnp, DS1_SYSID, City_Pair, WOSec)
{
this.id = id;
this.WorkOrder = wo;
this.pon = pon;
this.fnp = fnp;
this.DS1_SYSID = DS1_SYSID;
this.City_Pair = City_Pair;
this.WOSection = WOSec;
//alert("Constructing OTSRecord:\n" + id + "\n" + wo + "\n" + pon + "\n" + fnp);
}
var aOTSRecords = new Array(
new fnOTSRecord('2425','6817',
new fnOTSRecord('2425','6817',
new fnOTSRecord('6844','9587',
new fnOTSRecord('6845','009587
new fnOTSRecord('14226','13393
new fnOTSRecord('14271','01342
new fnOTSRecord('33943','22637
new fnOTSRecord('33944','22638
new fnOTSRecord('33945','22639
new fnOTSRecord('33946','22640
new fnOTSRecord('34804','23145
new fnOTSRecord('39756','25750
new fnOTSRecord('39757','02575
new fnOTSRecord('41028','02641
new fnOTSRecord('41028','02641
new fnOTSRecord('41561','26689
new fnOTSRecord('41561','26689
new fnOTSRecord('41643','26738
new fnOTSRecord('41644','02673
new fnOTSRecord('41644','02673
new fnOTSRecord('41645','02673
new fnOTSRecord('63394','40528
new fnOTSRecord('67496','43394
);
// functions to sort
function fnSortByID( arg1, arg2 )
{
if( arg1.ID == arg2.ID ) return 0;
return ( parseInt(arg1.ID) < parseInt(arg2.ID) ) ? sortLT : sortGT;
}
function fnSortByWorkOrder( arg1, arg2 )
{
if( arg1.WorkOrder == arg2.WorkOrder ) return 0;
return ( parseInt(arg1.WorkOrder) < parseInt(arg2.WorkOrder) ) ? sortLT : sortGT;
}
function fnSortByPon( arg1, arg2 )
{
if( arg1.pon == arg2.pon ) return 0;
return ( arg1.pon < arg2.pon ) ? sortLT : sortGT;
}
function fnSortByFnp( arg1, arg2 )
{
if( arg1.fnp == arg2.fnp ) return 0;
return ( parseInt(arg1.fnp) < parseInt(arg2.fnp) ) ? sortLT : sortGT;
}
function fnSortByDS1( arg1, arg2 )
{
if( arg1.DS1_SYSID == arg2.DS1_SYSID ) return 0;
return ( arg1.DS1_SYSID < arg2.DS1_SYSID ) ? sortLT : sortGT;
}
function fnSortByCity_Pair( arg1, arg2 )
{
if( arg1.City_Pair == arg2.City_Pair ) return 0;
return ( arg1.City_Pair < arg2.City_Pair ) ? sortLT : sortGT;
}
function fnSortByWOSection( arg1, arg2 )
{
if( arg1.WOSection == arg2.WOSection ) return 0;
return ( arg1.WOSection < arg2.WOSection ) ? sortLT : sortGT;
}
// Array of sort functions
aSortFuncs = new Array( fnSortByID, fnSortByWorkOrder, fnSortByPon, fnSortByFnp,
fnSortByDS1, fnSortByCity_Pair, fnSortByWOSection);
// Array of Field Names
aSortByNames = new Array( "ID", "Work Order", "PON", "FNP", "DS1 SYSID",
"City Pair", "WO-Section" );
//alert("sortGT & sortLT:\n" + sortGT + "\n" + sortLT);
function sortBy( fldNum )
{
//alert("sortGT & sortLT:\n" + sortGT + "\n" + sortLT + "\n" + fldNum + "\n" + lastSortBy );
if ( fldNum != lastSortBy )
{
sortGT = 1;
sortLT = -1;
lastSortBy = fldNum;
} else {
sortGT = - sortGT;
sortLT = - sortLT; // reverse the sort
}
// alert("sortGT & sortLT:\n" + sortGT + "\n" + sortLT + "\n" + fldNum + "\n" + lastSortBy + "\n" + parseInt(curItem.id) );
aOTSRecords.sort( aSortFuncs[fldNum] ); //sort
var HTMLText = "";
HTMLText += "<CENTER>\n<H3>List of OTS orders "
+ sortStyle[sortGT+1] + aSortByNames[fldNum] + "</H3><P>\n" ;
HTMLText += "<!-- <FONT Size='-1'><I>Click on a column title to sort by that column; click again to reverse the sort.</I></FONT>\n -->";
HTMLText += "<TABLE Width='100%' Border=0 CellPadding=3 bgColor=lightgreen>\n<TR>\
for( col = 0; col < aSortByNames.length; ++col )
{
HTMLText += ( " <TH><A HREF='javascript:sortBy(" + col
+ ")'>" + aSortByNames[col] + "</A></TH>\n" );
}
HTMLText += ( "</TR>\n" );
for( row = 0; row < aOTSRecords.length; ++row )
{
curItem = aOTSRecords[row];
/*
document.write( "<TR>\n" );
document.write( " <TD>" + parseInt(curItem.id) + "</TD>\n" );
document.write( " <TD>" + curItem.WorkOrder + "</TD>\n" );
document.write( " <TD>" + curItem.pon + "</TD>\n" );
document.write( " <TD>" + curItem.fnp + "</TD>\n" );
document.write( " <TD>" + curItem.DS1_SYSID + "</TD>\n" );
document.write( " <TD>" + curItem.City_Pair + "</TD>\n" );
document.write( " <TD>" + curItem.WOSection + "</TD>\n" );
document.write( "</TR>\n" );
*/
HTMLText += ( "<TR>\n" );
HTMLText += ( " <TD>" + parseInt(curItem.id) + "</TD>\n" );
HTMLText += ( " <TD>" + curItem.WorkOrder + "</TD>\n" );
HTMLText += ( " <TD>" + curItem.pon + "</TD>\n" );
HTMLText += ( " <TD>" + curItem.fnp + "</TD>\n" );
HTMLText += ( " <TD>" + curItem.DS1_SYSID + "</TD>\n" );
HTMLText += ( " <TD>" + curItem.City_Pair + "</TD>\n" );
HTMLText += ( " <TD>" + curItem.WOSection + "</TD>\n" );
HTMLText += ( "</TR>\n" );
}
HTMLText += ( "</TABLE>\n<P><HR>" );
HTMLText += (aSortFuncs[5] + "<br>");
HTMLText += (aOTSRecords[5].id + "\n");
writeLayer("MyDiv", HTMLText);
}
</SCRIPT>
</head>
<body>
<div id="MyDiv" style="position:absolute;l
</body>
<SCRIPT Language="JavaScript">
sortBy(1);
</SCRIPT>
</html>
You also might want to change
new fnOTSRecord('2425','6817',
to
new fnOTSRecord(2425,6817,0241
to eliminate the parsing in compare functions. I haven't tried it though.
Your ID sort doesn't work because you have mis-spelled id as ID in the compare function. Javascript is case-sensitive.
Business Accounts
Answer for Membership
by: knightEknightPosted on 2003-05-30 at 11:06:20ID: 8616156
>> The array is built during a pull from a db using SQL server.
why don't you just sort it using an ORDER BY clause in your SQL ?