• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with date fields Table sorting

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic