Date Sorting until "-"

Date Sorting until "-"

choggerchogger Posts: 15Questions: 4Answers: 0
edited January 2014 in Plug-ins
Hello everyone, happy new year

I am searching for a solution to sort date values which a in a column with other data. There is always a "-" after the date and the date is always the first string in the field. Is there a solution for this problem?

The page can be found here:
http://chogger.de/database/?p=driver&status=inactive

Replies

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    > 18.12.2013 - View Result Chicagoland Speedway

    is not a valid date since the whole string is looked at by default. You will need to create and use a sorting plug-in:

    http://datatables.net/development/sorting
    http://datatables.net/plug-ins/sorting

    Allan
  • choggerchogger Posts: 15Questions: 4Answers: 0
    edited January 2014
    I thought there would be a solution for this already.
    I am not sure how to start, but thank you Allan. I will try it
  • choggerchogger Posts: 15Questions: 4Answers: 0
    Wow, was easier than expected:

    [code]
    // chogger Custom Date Sorting

    function chogger_date(date) {
    var date = date.slice(0, 10);

    if (date.indexOf('.') > 0) {
    /*date a, format dd.mn.(yyyy) ; (year is optional)*/
    var chogger_date = date.split('.');
    } else {
    /*date a, format dd/mn/(yyyy) ; (year is optional)*/
    var chogger_date = date.split('/');
    }

    /*year (optional)*/
    if (chogger_date[2]) {
    var year = chogger_date[2];
    } else {
    var year = 0;
    }

    /*month*/
    var month = chogger_date[1];
    if (month.length == 1) {
    month = 0+month;
    }

    /*day*/
    var day = chogger_date[0];
    if (day.length == 1) {
    day = 0+day;
    }

    return (year + month + day) * 1;
    }

    jQuery.fn.dataTableExt.oSort['chogger_date-asc'] = function(a, b) {
    x = chogger_date(a);
    y = chogger_date(b);

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

    jQuery.fn.dataTableExt.oSort['chogger_date-desc'] = function(a, b) {
    x = chogger_date(a);
    y = chogger_date(b);

    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };
    [/code]
  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Excellent to hear :-). Nice solution!

    Allan
This discussion has been closed.