Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 7th, 2005, 3:43 AM   #1
crmpicco
Newbie
 
Join Date: Feb 2005
Posts: 13
Rep Power: 0 crmpicco is on a distinguished road
Exclamation parseInt / parseFloat

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:

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'><font face='verdana' size=1>ID</font></a></td>" + // The Contract ID (e.g. 244)
"<td align='left'><a href='#' onclick=\"sortAsc(1,'s')\" title='Sort by Airline'><font face='verdana' size=1>A/L</font></a></td>"+ // The Airline Code (e.g. AF)
"<td align='left'><a href='#' onclick=\"sortAsc(2,'s')\" title='Sort by Contract Code'><font face='verdana' size=1>Contract Code</font></a></td>"+ // The Contract Code
"<td align='left'><a href='#' onclick=\"sortAsc(3,'s')\" title='Sort by Remark'><font face='verdana' size=1>Remark</font></a></td>"+ // The Remark
"<td align='left'><a href='#' onclick=\"sortAsc(4,'s')\" title='Sort by Status'><font face='verdana' size=1>Status</font></a></td>"+ // The Status (e.g. Confirmed)
"<td align='left'><a href='#' onclick=\"sortAsc(5,'s')\" title='Sort by Date Loaded'><font face='verdana' size=1>Date Loaded</font></a></td>"+ // Date Loaded (05-MAR-2005)
"<td align='left'><a href='#' onclick=\"sortAsc(6,'s')\" title='Sort by Expiry Date'><font face='verdana' size=1>Expiry Date</font></a></td>"+ // Expiry Date 
"<td align='left'><a href='#' onclick=\"sortAsc(7,'s')\" title='Sort by Contract Title'><font face='verdana' size=1>Contract Title</font></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 + "'><font face='verdana' size='1'>" + stxt +"</font></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'>"+
        "<font face='verdana' size='1'>"+
        "<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></font></td>"+
        "<td align='left'><font face='verdana' size='1'>" + arr[f][1] + "</font></td>"+
        "<td align='left'><font face='verdana' size='1'>" + arr[f][2] + "</font></td>"+
        "<td align='left'><font face='verdana' size='1'>" + arr[f][3] + "</font></td>"+
        "<td align='left'><font face='verdana' size='1'>" + arr[f][4] + "</font></td>"+
        "<td align='left'><font face='verdana' size='1'>" + arr[f][5] + "</font></td>"+
        "<td align='left'><font face='verdana' size='1'>" + arr[f][6] + "</font></td>"+
        "<td align='left'><font face='verdana' size='1'>" + arr[f][7] + "</font></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?
crmpicco is offline   Reply With Quote
Old Jun 25th, 2005, 11:26 AM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Firstly: why are you using a JavaScript-coded sort function? It will be 100s of times slower than the sort function provided by to the JavaScript interpreter via Array.sort().

It's sorting alphabetically in ASCII, not by integer value (I would have thought the function name, 'sortAsc', would have been a clue here). The JavaScript Array.sort function also does the same, but you can pass it a function as a parameter that will be used in evaluating how to sort the list.
So, to sort a bunch of numbers:
var nums = new Array(32, 23, 5, 49, 82, 245, 5, 1);
nums.sort(function(a, b) { return a - b; });
// nums is now sorted.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC