Custom sorting strange problem


I have a very strange probelm with custom sorting - its driving me mad as I cannot see why it is happening? The column has a date format of the following.

dd/mm/yyyy hh:ii:ss


31/01/2008 05:01:09

my custom sorting code is thus.


    var aparts= a.split(" “);

    var datepart=aparts[0].split(”/");

    var timepart=aparts[1].split(":");

    var adate=new Date(datepart[2], datepart[1], datepart[0], timepart[0], timepart[1], timepart[2]);


    var bparts= b.split(" “);

    var datepartb=bparts[0].split(”/");

    var timepartb=bparts[1].split(":");

    var bdate=new Date(datepartb[2], datepartb[1], datepartb[0], timepartb[0], timepartb[1], timepartb[2]);


if (order==“asc”) return (adate>bdate?1:-1);

else return (adate<bdate?1:-1);


this appeasr to have been working fine until a few days ago somebody noticed the following in the list when sorted asc

02/02/2008 05:00:11

31/01/2008 05:01:09

02/02/2008 05:05:55

which is obviously wrong - its seems to be the only one that is but I cannot for the life of me see why it should be there? Is there something glaring that I am missing?


The Date object expect that month and date will be a zero based numbers, so you need to use

var adate=new Date(datepart[2]-1, datepart[1]-1, datepart[0], timepart[0], timepart[1], timepart[2]);

var bdate=new Date(datepartb[2]-1, datepartb[1]-1, datepartb[0], timepartb[0], timepartb[1], timepartb[2]);