Wrox Programmer Forums parseInt - sort ascending
 |
 Javascript How-To Ask your "How do I do this with Javascript?" questions here.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

June 7th, 2005, 03:37 AM
 crmpicco Friend of Wrox Join Date: Jan 2005 Posts: 1,525 Thanks: 0 Thanked 0 Times in 0 Posts
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(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
///////////////////////////////////////////
}
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.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

June 8th, 2005, 04:27 AM
 joefawcett Wrox Author Join Date: Jun 2003 Posts: 3,074 Thanks: 1 Thanked 38 Times in 37 Posts

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)

July 4th, 2005, 10:28 AM
 crmpicco Friend of Wrox Join Date: Jan 2005 Posts: 1,525 Thanks: 0 Thanked 0 Times in 0 Posts

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.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt

July 5th, 2005, 09:13 AM
 crmpicco Friend of Wrox Join Date: Jan 2005 Posts: 1,525 Thanks: 0 Thanked 0 Times in 0 Posts

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.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt

 Similar Threads Thread Thread Starter Forum Replies Last Post how to sort an array ascending, help beccali Visual C++ 1 March 26th, 2007 05:19 PM number not finding in ascending order mankoti_mankoti2000 Access 1 January 19th, 2007 01:07 AM Listing dates ascending lgschulz VB.NET 2002/2003 Basics 1 December 19th, 2006 09:22 AM how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM Sort ascending and descending bertcox XSLT 1 May 17th, 2004 05:36 AM