UK Date Sorting, odd behaviour

UK Date Sorting, odd behaviour

nicgordonnicgordon Posts: 2Questions: 0Answers: 0
edited October 2010 in Plug-ins
Hi guys,
I implemented the UK date sorting and it didn't quite seem to be behaving the way I expected it to. So I checked the source code:

[code]
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

return ((x < y) ? -1 : ((x > y) ? 1 : 0));
[/code]

How was this intended to work? From what I can see it just adds the year to the month to the day and then compares those? Perhaps I am missing something. Can someone please explain it to me?

It seems that 1/1/2010 would equal 2012 (1+1+2010) and that would be less than 15/11/1999 (15+11+1999=2025). Way inaccurate.

I rewrote it anyway. My code is very verbose but I know it works. Someone else can code it into shorthand:

[code]
jQuery.fn.dataTableExt.oSort['au-date-asc'] = function(a,b) {
var auDatea = a.split('/');
var auDateb = b.split('/');

if (auDatea[2] > auDateb[2])
{
return 1;
}
else if (auDatea[2] < auDateb[2])
{
return -1;
}
else
{
if (auDatea[1] > auDateb[1])
{
return 1;
}
else if (auDatea[1] < auDateb[1])
{
return -1;
}
else
{
if (auDatea[0] > auDateb[0])
{
return 1;
}
else if (auDatea[0] < auDateb[0])
{
return -1;
}
else
{
return 0;
}
}
}

};

jQuery.fn.dataTableExt.oSort['au-date-desc'] = function(a,b) {
var auDatea = a.split('/');
var auDateb = b.split('/');

if (auDatea[2] > auDateb[2])
{
return -1;
}
else if (auDatea[2] < auDateb[2])
{
return 1;
}
else
{
if (auDatea[1] > auDateb[1])
{
return -1;
}
else if (auDatea[1] < auDateb[1])
{
return 1;
}
else
{
if (auDatea[0] > auDateb[0])
{
return -1;
}
else if (auDatea[0] < auDateb[0])
{
return 1;
}
else
{
return 0;
}
}
}

};
[/code]

Cheers

Replies

  • woodstownwoodstown Posts: 2Questions: 0Answers: 0
    edited October 2010
    "ow was this intended to work? From what I can see it just adds the year to the month to the day and then compares those? Perhaps I am missing something. Can someone please explain it to me?

    It seems that 1/1/2010 would equal 2012 (1+1+2010) and that would be less than 15/11/1999 (15+11+1999=2025). Way inaccurate."

    I think you misunderstood something here.

    1/1/2010 would be : 20100101 which is more that 19991115. he handles the dates as strings, thats why you got the "*1" at the end. to convert it back to numbers.

    for me the ukdate script works fine, even with german dates. make sure you have the dated splited correctly here:
    [code]
    dateMin = dateRange.substring(6,10) + dateRange.substring(3,5) + dateRange.substring(0,2);
    dateMax = dateRange.substring(19,23) + dateRange.substring(16,18) + dateRange.substring(13,15) ;
    [/code]
    the code above is for dates in the format: 31-01-2010
  • nicgordonnicgordon Posts: 2Questions: 0Answers: 0
    Ah yes, you are quite right. Thanks for the explanation. I wonder why I got the strange sort order in the first place? I'll have to run some more tests.
    Cheers!
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    woodstown is quite correct, this plug-in works on string concatenation rather than adding numbers together. Having said that, a date such as 1 / 1 / 2000 probably won't give exactly the desired effect since it would be rendered as 200011 (not 20000101). As such, I think there is actually still a bug here.

    Allan
This discussion has been closed.