UK Date TypeDetection and SortyBy

UK Date TypeDetection and SortyBy

ChalisChalis Posts: 4Questions: 0Answers: 0
edited September 2009 in General
I'm trying to sort a column with dates in the format of dd/mm/yyyy (d/m/Y) and although I've pieced together something from the Plugins section, i'm not sure if I've done it correctly as it doesn't seem to be working.

[code]
jQuery.fn.dataTableExt.aTypes.push(
function(sData){
if (sData.match(/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/)){
return 'date_eu';
}
return null;
}
);

jQuery.fn.dataTableExt.oSort['date_eu-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));
};

jQuery.fn.dataTableExt.oSort['date_eu-desc'] = 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));
};

$(document).ready(function(){
$('#example').dataTable({
"bStateSave": true
});
});
[/code]

I also tried it with the default date format supplied with the plugin (dd/mm/yy) and still had no success. The only modification to the code from the plugins section is the regex string and the id.

Replies

  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    Hi Chalis ,

    Everything looks okay there as far as I can tell. Do you have a link you can post showing the problem? That would make debugging much easier.

    Thanks,
    Allan
  • calumbrodiecalumbrodie Posts: 1Questions: 0Answers: 0
    edited November 2009
    Hi Allan,

    I have just used this UK_Date plugin also and found that it only worked if I amended it by removing the *1 on both returned variables....

    [code]
    var ukDatea = a.split('/');
    var ukDateb = b.split('/');
    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    console.log(x);
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
    console.log(y);
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    [/code]
    Firebug returns NaN for both console.logs

    [code]
    var ukDatea = a.split('/');
    var ukDateb = b.split('/');
    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]);
    console.log(x);
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]);
    console.log(y);
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    [/code]
    Firebug returns numbers for both console.logs

    Sorry I can't provide a test URL because this is an intranet app but I can provide you with more information if needed.
    Regards
    Calum
  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    Hi Calum,

    That's interesting - does this work for all browsers? Perhaps it should actually be:

    [code]
    var x = parseInt(ukDatea[2] + ukDatea[1] + ukDatea[0]);
    [/code]
    Otherwise I think there is a risk of string comparisons, rather than numeric. Although with Javascript's loose typing, it's hard to say... It would be interesting to know what you think of this, and I'll update the plug-in code accordingly.

    Regards,
    Allan
This discussion has been closed.