uk_date sorting plugin

uk_date sorting plugin

BCageBCage Posts: 3Questions: 0Answers: 0
edited January 2012 in Plug-ins
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]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    edited January 2012
    > 01/12/2011, since 30+1+2011=2043

    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
  • BCageBCage Posts: 3Questions: 0Answers: 0
    Thank you, that explains it. I guess I implemented it wrong at first, since I got incorrect results. Should have figured this out myself.

    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.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    I like the design of the "universal date plugin" which lets you configure the order of your date fields:

    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.
  • BCageBCage Posts: 3Questions: 0Answers: 0
    edited January 2012
    Thanks, that one looks nice and would have worked also. I'll keep that plug-in in mind for future projects.
This discussion has been closed.