p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   parseInt - sort ascending (http://p2p.wrox.com/showthread.php?t=30163)

crmpicco June 7th, 2005 03:37 AM

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

joefawcett June 8th, 2005 04:27 AM

Shouldn't you be passing 'i' as the second parameter if you want a numeric sort?
As a side issue never use parseInt without the secoond argument:
Code:

var int = parseInt(value, 10);
NOT
Code:

var int = parseInt(value);
if you don't specify you can weird results try:
Code:

alert(parseInt("08"))
for instance.
Better to use Math.floor(value) instead anyway.


--

Joe (Microsoft MVP - XML)

crmpicco July 4th, 2005 10:28 AM

Code:

Math.floor(infcounter)
is that the best way to change this value to a number?

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

crmpicco July 5th, 2005 09:13 AM

I have been using Math.ceil, Math.abs and Math.floor. But is parseInt just as accurate?

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


All times are GMT -4. The time now is 07:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.