Sorting by date (DD.MM.YYYY)

Sorting by date (DD.MM.YYYY)

afarberafarber Posts: 53Questions: 0Answers: 0
edited June 2011 in General
Hello,

please point me to the right direction on how to implement sorting by dates (like 31.12.2011)

Here is the page where I currently have problems: http://preferans.de/user.php?id=DE7692

Thank you!
Alex

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi Alex,

    A small modification to this plug-in (dots rather than slashes) will do it: http://datatables.net/plug-ins/sorting#date_euro_short

    Allan
  • afarberafarber Posts: 53Questions: 0Answers: 0
    Thank you, but could I maybe as alternative add an hidden column with numeric timestamps?

    I don't know how to use them for sorting though... (i.e. when clicking the Date column, the hidden timestamps column should be used for sorting)

    Regards
    Alex
  • afarberafarber Posts: 53Questions: 0Answers: 0
    Hello Allan,

    I've looked at the plugin you've suggested, but don't understand what would 'uk_date-desc' string mean? How do I use it in my case?

    Also after looking at http://www.datatables.net/usage/columns I have added this code:


    $("#comments").dataTable( {
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "aoColumns": [
    /* day */ { "bSearchable": false, "bSortable": true, "sType": "date" },
    /* about */ { "bSearchable": true, "bSortable": false },
    /* author */ { "bSearchable": false, "bSortable": false }
    ],

    but then the 1st column day isn't sorting anything (I click it, the v changes to ^, but nothing happens). Is it because JavaScript's date() doesn't recognize DD.MM.YYYY? I don't see any errors in the console.

    Thank you
    Alex
  • afarberafarber Posts: 53Questions: 0Answers: 0
    I've figured it out, thanks for the pointers:



    $(function() {
    jQuery.fn.dataTableExt.oSort['day-asc'] = function(a, b) {
    var daya = a.split('.');
    var dayb = b.split('.');

    var x = (daya[2] + daya[1] + daya[0]) * 1;
    var y = (dayb[2] + dayb[1] + dayb[0]) * 1;

    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['day-desc'] = function(a, b) {
    var daya = a.split('.');
    var dayb = b.split('.');

    var x = (daya[2] + daya[1] + daya[0]) * 1;
    var y = (dayb[2] + dayb[1] + dayb[0]) * 1;

    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };

    $("#comments").dataTable( {
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "aaSorting": [[0, "desc"]],
    "aoColumns": [
    /* day */ { "bSearchable": false, "bSortable": true, "sType": "day" },
    /* about */ { "bSearchable": true, "bSortable": false },
    /* author */ { "bSearchable": false, "bSortable": false }
    ]
    }
    });
This discussion has been closed.