uk_date sorting plugin
uk_date sorting plugin
Could someone please explain to me how the plug-in for sorting the uk_dates is supposed to work? All it seems to do is add all the individual date parts and compare them. Wouldn't this have the result of the plug-in treating 30/01/2011 as a later date than 01/12/2011, since 30+1+2011=2043 is larger than 1+12+2011=2024? Here is the code for the original plug-in (only the ascending part):
[code]
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
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]
I am using the following code to compare uk dates, which seems to work fine. It treats empty dates as 01/01/1900.
[code]
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
a = a == "" ? "01/01/1900" : a;
b = b == "" ? "01/01/1900" : b;
var ukDatea = a.split('/');
var ukDateb = b.split('/');
if(ukDatea[2] < ukDateb[2]) return -1;
else if(ukDatea[2] > ukDateb[2]) return 1;
if(ukDatea[1] < ukDateb[1]) return -1;
else if(ukDatea[1] > ukDateb[1]) return 1;
if(ukDatea[0] < ukDateb[0]) return -1;
else if(ukDatea[0] > ukDateb[0]) return 1;
return 0;
};
jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
a = a == "" ? "01/01/1900" : a;
b = b == "" ? "01/01/1900" : b;
var ukDatea = a.split('/');
var ukDateb = b.split('/');
if(ukDatea[2] < ukDateb[2]) return 1;
else if(ukDatea[2] > ukDateb[2]) return -1;
if(ukDatea[1] < ukDateb[1]) return 1;
else if(ukDatea[1] > ukDateb[1]) return -1;
if(ukDatea[0] < ukDateb[0]) return 1;
else if(ukDatea[0] > ukDateb[0]) return -1;
return 0;
};
[/code]
[code]
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
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]
I am using the following code to compare uk dates, which seems to work fine. It treats empty dates as 01/01/1900.
[code]
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
a = a == "" ? "01/01/1900" : a;
b = b == "" ? "01/01/1900" : b;
var ukDatea = a.split('/');
var ukDateb = b.split('/');
if(ukDatea[2] < ukDateb[2]) return -1;
else if(ukDatea[2] > ukDateb[2]) return 1;
if(ukDatea[1] < ukDateb[1]) return -1;
else if(ukDatea[1] > ukDateb[1]) return 1;
if(ukDatea[0] < ukDateb[0]) return -1;
else if(ukDatea[0] > ukDateb[0]) return 1;
return 0;
};
jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
a = a == "" ? "01/01/1900" : a;
b = b == "" ? "01/01/1900" : b;
var ukDatea = a.split('/');
var ukDateb = b.split('/');
if(ukDatea[2] < ukDateb[2]) return 1;
else if(ukDatea[2] > ukDateb[2]) return -1;
if(ukDatea[1] < ukDateb[1]) return 1;
else if(ukDatea[1] > ukDateb[1]) return -1;
if(ukDatea[0] < ukDateb[0]) return 1;
else if(ukDatea[0] > ukDateb[0]) return -1;
return 0;
};
[/code]
This discussion has been closed.
Replies
You would be correct if the values were integers - but they aren't - they are strings (as a result of the 'split' on a string). Hence rather than numerical addition the "+" operator in this case it is doing string concatenation. As a such you should end up with something like:
> 20110130
which is then converted to a number and compare those results.
The problem with doing it all numerically would be that something like "01/04/2011" and "01/01/2012" would give an in correct result - as you rightly point out.
The best solution would be to convert everything to a 'Date()' object, but that can be a little bit slower than the uk_date 'solution' - to really that is an optimisation for known parameters (i.e. the date is in the format DD/MM/YYYY).
Allan
I do need a plug-in however since the format I'm actually using is DD-MM-YYYY and I don't think Date() recognizes that format.
http://datatables.net/forums/discussion/6587/unidate-universal-date-sorting-plugin/p1
it also splits the fields on ANY non-numeric character (space, colon, slash, backslash, etc) in any combination.
so you could be using "2012.01.03 12.30.59" or "2012/01\ 03 12;arf.30:^%&$59" and still get it right.