UK Date Sorting, odd behaviour
UK Date Sorting, odd behaviour
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
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
This discussion has been closed.
Replies
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
Cheers!
Allan