This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes HTML, CSS and JavaScript and the fly likes problem with date fields  Table sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem with date fields  Table sorting " Watch "problem with date fields  Table sorting " New topic
Author

problem with date fields Table sorting

sirisha makkapati
Ranch Hand

Joined: Dec 10, 2006
Posts: 83
Hello friends,
I am doing table sorting with javascript.
I wanted to sort date fields.
can any body give me an idea how to add code to sort date fields to my scipt.

My script is as follows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>


Thanks in advance.

<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/

function zxcSortTable(zxcid,zxccnus){
var zxcargs=zxcSortTable.arguments
var zxctable=document.getElementById(zxcid);
var zxcrows=zxctable.rows,zxcscells=[],zxcclones=[],zxcblanks=[],zxccnt=0;
for (var zxc0=1;zxc0<zxcargs.length;zxc0++){
for (var zxc0a=1;zxc0a<zxcrows.length;zxc0a++){
var zxccells=zxcrows[zxc0a].cells;
zxcscells.push(zxccells[zxcargs[zxc0]]);
var zxcdata=zxccells[zxcargs[zxc0]].firstChild.data;
if (/[a-z]|[0-9]/i.test(zxcdata)){
zxcclones[zxccnt]=(zxccells[zxcargs[zxc0]].cloneNode(true));
zxcclones[zxccnt++].data=zxcdata;
}
else{
zxcblanks.push(zxccells[zxcargs[zxc0]].cloneNode(true));
}
}
}
if (!zxcrows[0].cells[zxcargs[1]].ud) zxcrows[0].cells[zxcargs[1]].ud='Up';
zxcrows[0].cells[zxcargs[1]].ud=(zxcrows[0].cells[zxcargs[1]].ud=='Up')?'Down':'Up';
zxcclones=zxcclones.sort(zxcSortTxt);
zxcclones=zxcclones.sort(zxcSortTxt);
if (zxcrows[0].cells[zxcargs[1]].ud=='Up') zxcclones=zxcclones.reverse();
zxcclones=zxcclones.concat(zxcblanks);
for (var zxc1=0;zxc1<zxcclones.length;zxc1++){
zxcscells[zxc1].parentNode.replaceChild(zxcclones[zxc1],zxcscells[zxc1]);
}
for (var zxc2=1;zxc2<zxcargs.length;zxc2++){
zxcrows[0].cells[zxcargs[zxc2]].getElementsByTagName('IMG')[0].src=(zxcrows[0].cells[zxcargs[1]].ud=='Up')?'http://www.vicsjavascripts.org.uk/StdImages/down[1].gif':'http://www.vicsjavascripts.org.uk/StdImages/up[1].gif';
}
}

function zxcSortTxt(zxca,zxcb){
var zxcaa=zxca.firstChild.data;
var zxcbb=zxcb.firstChild.data;
if (zxcaa>zxcbb) return 1;
if (zxcaa<zxcbb) return -1;
return 0;
}
/*]]>*/
</script>
</head>


Thanks in advance.
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
I think you need to turn you dates into a yyyy-mm-dd string that will sort properly like this:

da = new Date(document.lastModified) // Create a Date Object set to the last modifed date
dy = da.getFullYear() // Get full year (as opposed to last two digits only)
dm = da.getMonth() + 1 // Get month and correct it (getMonth() returns 0 to 11)
dd = da.getDate() // Get date within month
if ( dy < 1970 ) dy = dy + 100; // We still have to fix the millennium bug
ys = new String(dy) // Convert year, month and date to strings
ms = new String(dm)
ds = new String(dd)
if ( ms.length == 1 ) ms = "0" + ms; // Add leading zeros to month and date if required
if ( ds.length == 1 ) ds = "0" + ds;
ys = ys + "-" + ms + "-" + ds // Combine year, month and date in ISO format
document.write ( "Last Updated " + ys ) // Display the result

Thanks to http://home.clara.net/shotover/datetest.htm
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You would have to change the zxcSortTxt.

Here is the basic idea, you would have to figure out how to apply it:




Eric
[ January 22, 2008: Message edited by: Eric Pascarello ]
 
jQuery in Action, 2nd edition
 
subject: problem with date fields Table sorting