View Single Post
  #1 (permalink)  
Old June 7th, 2005, 03:37 AM
crmpicco crmpicco is offline
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default parseInt - sort ascending

Code:
function sortAsc(sub,dtype)
{

    var tmpArr=new Array();
    var arrQa=new Array();
    for(i=0;i<arr.length;i++)
    {
        tmpArr[i]=arr[i][sub];
        arrQa[i]=arr[i][1];
        seq[i]=i;
    }

    var tmp;
    var tmp1;
    for(j=0;j<tmpArr.length;j++)
    {
        for(k=0;k<tmpArr.length-1;k++)
        {

            if(dtype=='i')
            {
                if(parseInt(tmpArr[k])>parseInt(tmpArr[k+1]))
                {
                    tmp=tmpArr[k+1];
                    tmpArr[k+1]=tmpArr[k];
                    tmpArr[k]=tmp;

                    tmpQa=arrQa[k+1];
                    arrQa[k+1]=arrQa[k];
                    arrQa[k]=tmpQa;

                    tmp1=seq[k+1];
                    seq[k+1]=seq[k];
                    seq[k]=tmp1;    
                }
                else if(parseInt(tmpArr[k])==parseInt(tmpArr[k+1]))
                {
                    if(arrQa[k]>arrQa[k+1])
                    {
                        tmp=tmpArr[k+1];
                        tmpArr[k+1]=tmpArr[k];
                        tmpArr[k]=tmp;

                        tmpQa=arrQa[k+1];
                        arrQa[k+1]=arrQa[k];
                        arrQa[k]=tmpQa;

                        tmp1=seq[k+1];
                        seq[k+1]=seq[k];
                        seq[k]=tmp1;    
                    }
                }
            }

            if(dtype=='s')
            {
                if(tmpArr[k]>tmpArr[k+1])
                {
                    tmp=tmpArr[k+1];
                    tmpArr[k+1]=tmpArr[k];
                    tmpArr[k]=tmp;

                    tmpQa=arrQa[k+1];
                    arrQa[k+1]=arrQa[k];
                    arrQa[k]=tmpQa;

                    tmp1=seq[k+1];
                    seq[k+1]=seq[k];
                    seq[k]=tmp1;    
                }
                else if(tmpArr[k]==tmpArr[k+1])
                {
                    if(arrQa[k]>arrQa[k+1])
                    {
                        tmp=tmpArr[k+1];
                        tmpArr[k+1]=tmpArr[k];
                        tmpArr[k]=tmp;

                        tmpQa=arrQa[k+1];
                        arrQa[k+1]=arrQa[k];
                        arrQa[k]=tmpQa;

                        tmp1=seq[k+1];
                        seq[k+1]=seq[k];
                        seq[k]=tmp1;    
                    }
                }

            }

            if(dtype=='d')
            {
                var d1=new Date(tmpArr[k]);
                var d2=new Date(tmpArr[k+1]);

                if(d1>d2)
                {
                    tmp=tmpArr[k+1];
                    tmpArr[k+1]=tmpArr[k];
                    tmpArr[k]=tmp;

                    tmpQa=arrQa[k+1];
                    arrQa[k+1]=arrQa[k];
                    arrQa[k]=tmpQa;

                    tmp1=seq[k+1];
                    seq[k+1]=seq[k];
                    seq[k]=tmp1;    
                }
                else if(d1.valueOf()==d2.valueOf())
                {
                    if(arrQa[k]>arrQa[k+1])
                    {
                        tmp=tmpArr[k+1];
                        tmpArr[k+1]=tmpArr[k];
                        tmpArr[k]=tmp;

                        tmpQa=arrQa[k+1];
                        arrQa[k+1]=arrQa[k];
                        arrQa[k]=tmpQa;

                        tmp1=seq[k+1];
                        seq[k+1]=seq[k];
                        seq[k]=tmp1;
                    }
                }
            }

        }
    }
    maketable();
}
I have that JS code that sorts columns Ascending.
This is where the table is built:
Code:
function maketable(edit)
{
str="<table width='100%' cellspacing='1' cellpadding='2' border='1' align='center'>"+
"<tr bgcolor='#ececec'bordercolor='#cccccc'>"+
"<td align='left'><a href='#' onclick=\"sortAsc(0,'s')\" title='Sort by ID'>ID</a></td>" + // The Contract ID (e.g. 244)
"<td align='left'><a href='#' onclick=\"sortAsc(1,'s')\" title='Sort by Airline'>A/L</a></td>"+ // The Airline Code (e.g. AF)
"<td align='left'><a href='#' onclick=\"sortAsc(2,'s')\" title='Sort by Contract Code'>Contract Code</a></td>"+ // The Contract Code
"<td align='left'><a href='#' onclick=\"sortAsc(3,'s')\" title='Sort by Remark'>Remark</a></td>"+ // The Remark
"<td align='left'><a href='#' onclick=\"sortAsc(4,'s')\" title='Sort by Status'>Status</a></td>"+ // The Status (e.g. Confirmed)
"<td align='left'><a href='#' onclick=\"sortAsc(5,'s')\" title='Sort by Date Loaded'>Date Loaded</a></td>"+ // Date Loaded (05-MAR-2005)
"<td align='left'><a href='#' onclick=\"sortAsc(6,'s')\" title='Sort by Expiry Date'>Expiry Date</a></td>"+ // Expiry Date 
"<td align='left'><a href='#' onclick=\"sortAsc(7,'s')\" title='Sort by Contract Title'>Contract Title</a></td>"+ // The Contract Title
"</tr>"

    var curdes="<%=curdes%>";
    for(i=0;i<seq.length;i++)
    {
        f=seq[i];
        regsel="";
        norsel="";
        optsel="";
        trasel="";
        buttval="";
        callfunc="";
        stxt="";
        saux="";
        if(arr[f][12]!="")
        {
             saux+=arr[f][12].toUpperCase( );
        }
        if(arr[f][13]!="")
        {
             saux+=".";
             saux+=arr[f][13].toUpperCase( );
        }
        if(arr[f][10].toUpperCase( )=="REPEAT")
        {
            regsel="selected";
            stxt="Repeat";
        }    
        else 
        {
            norsel="selected";
            stxt="TEST";
        }

        if(edit==true)
        {
            txvis="hidden";
            txpos="absolute"
            opvis="visible";
            oppos="";
            buttval="Update";
            callfunc="update_regtrip()";
        }
        else
        {
            txvis="visible";
            txpos="";
            opvis="hidden";
            oppos="absolute";
            buttval="Edit";
            callfunc="maketable(true)";
        }
        statustxt="<div style='visibility:" + txvis + ";position:" + txpos + "'>" + stxt +"</div>"
        statustxt+="<div style='visibility:" + opvis + ";position:" + oppos + "'><input type='hidden' name='trip" + i + "' value='" + arr[f][1] + "' ><select name='tripstat" + i + "'><option value='Repeat'" + regsel + ">Repeat</option><option value='Confirmed' " + norsel + ">Confirmed</option><option value='Option' " + optsel + ">Option</option><option value='Trash' " + trasel + ">Trash</option></select></div>"

        str+="<tr bgcolor='#ececec' bordercolor='#cccccc'>" 
        str+="<td align='left'>"+
        ""+
        "<a href='display_fare_search2.asp?air_cds="+ arr[f][1] +"&contract_id="+ arr[f][0] +"&contract_title="+arr[f][7]+"'>" + parseInt(arr[f][0]) + "</a></td>"+
        "<td align='left'>" + arr[f][1] + "</td>"+
        "<td align='left'>" + arr[f][2] + "</td>"+
        "<td align='left'>" + arr[f][3] + "</td>"+
        "<td align='left'>" + arr[f][4] + "</td>"+
        "<td align='left'>" + arr[f][5] + "</td>"+
        "<td align='left'>" + arr[f][6] + "</td>"+
        "<td align='left'>" + arr[f][7] + "</td>"
        str+="</tr>"

        ///////////////////////////////////////////
        // Array Values
        //alert (arr[f][0]) Contract ID
        //alert (arr[f][1]) Airline Code
        //alert (arr[f][2]) Contract Code
        //alert (arr[f][3]) Remark
        //alert (arr[f][4]) Status
        //alert (arr[f][5])// Date Loaded
        //alert (arr[f][6]) Date Expiry
        //alert (arr[f][7]) Contract Title
        ///////////////////////////////////////////
    }
    str+="<tr><td></td></tr></table>"
    document.all.tt.innerHTML=str;

}
The problem I am having is that when i sort an NUMBERS it sorts it like this
1
11
101
103
etc....

But i need
1
2
3
4
5
etc...

How can i sort it to stop this?

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
Reply With Quote