Another couple of sorting questions:

Another couple of sorting questions:

BelisariusBelisarius Posts: 26Questions: 0Answers: 0
edited March 2009 in General
I have a column of dates but it doesn't seem to sort by date (dd/mm/yyy). Is there something I need to set for that column to enable this?

I also have a column with format: d/xxx/ddd where d=1-0 and x=A-Z. I need to be able to sort on this columns basically on each part in turn with the d and ddd being integers and the xxx text. Suppose this would be similar to a date dd/mmm/yyyy sort?

I've looked at some of the alternate sorting solutions but I think I'm missing the basics of where to start with this one.

Any help much appreciated.

Cheers

Replies

  • BelisariusBelisarius Posts: 26Questions: 0Answers: 0
    Changing date format to yyyy/mm/dd works (supported javascript date format).

    Easiest way to use hidden column and sort on that?

    Similar for other column?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Belisarius,

    The key with using a sort function to sort dates which can't be parsed by Date().parse() is to transform the date into a number which can be sorted on (for example Unix time). Have a look at the thread here which discusses sorting dates: http://datatables.net/forums/comments.php?DiscussionID=81&page=1

    Allan
  • BelisariusBelisarius Posts: 26Questions: 0Answers: 0
    OK. I have the one sort column working OK for d/xxx/ddd and it ids it correctly and sorts correctly.

    I'm now trying to get the dd/mm/yyyy date to work. I've written an sType function similar to above but it seems to be setting the default sType to 'date' as it recognises e.g. 01/04/2009 as a valid javascript date (4th Jan 2009) and so never calls my functions. Is there anyway I can get it to call my sType function before the defaults?

    Assuming that is what is happening....
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    If you have a sorting function for a particular type, then you need to tell that column that it should use that column. Have a look at http://www.datatables.net/usage#sType .

    Also worth noting that "01/04/2009" will only be captured by Javascript Date().parse() under certain circumstances (i believe it is non-standard) - internalisation can cause problems here - for example I read it as 1st April 2009 ;-)

    Allan
  • BelisariusBelisarius Posts: 26Questions: 0Answers: 0
    I've added the stype entries as you suggest and it seems to work. However, it does seem to work for d/xxx/ddd (d=digit, x=letter) without it. I put a breakpoint in the type validation code i created and it seems to call it for each of the first entries in each column. When I do the same for the date column it never gets there as I assumed it was (in most cases) being trapped in the default date by Date().parse()?

    I've since switched the date to be dd-mmm-yyyy again that seems to work without the sType value as well?

    Cheers
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Belisarius,

    The Date().parse() built into most browsers can be quite fiddly because some browser makers extend what it can parse beyond what the specs say - so it can be quite hard to pin this kind of thing down. So yes, it is indeed quite possible that these formats will parse (I would expect dd-mmm-yyyy to certainly). It might be worth having a look at:

    MDC: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/parse
    ECMA Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf - section 15.9.4.2

    Allan
  • BelisariusBelisarius Posts: 26Questions: 0Answers: 0
    edited April 2009
    I think this is the validate/sort code for dd/mm/yyyy date formats. It seems to work and avoid the need for a hidden column.

    You need an entry in your datatable init code like:
    [code]
    "aoColumns": [
    { "bSortable": true, "sType": "box_ref"},
    { "bSortable": false , "bSearchable": false },
    { "bSortable": true, "sType": "uk_date2" },
    { "bSortable": false , "bSearchable": false },
    { "bSortable": false , "bSearchable": false },
    { "bSortable": false , "bSearchable": false },
    { "bSortable": false , "bSearchable": false }
    ]
    [/code]

    and then the following functions:

    [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|21)\d\d$/))
    {
    return 'uk_date2';
    }
    else
    {
    return null;
    }
    }
    );

    jQuery.fn.dataTableExt.oSort['uk_date2-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['uk_date2-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));
    };
    [/code]
    I'd appreciate any comments if it's not right

    Cheers
    NB Just seen some more info now in the Developer Section for this so hopefully it's right!
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Belisarius,

    Looks great to me! Nice one :-) Do you mind if I include this on the plug-ins page? If not, do you have a web-site you want me to point a link at (and name?).

    One thing worth noting is that if the type detection is working, then there should be no needed to explicitly defined 'sType' for the matching column. The idea being that it's not always ideal to define aoColumns (although in your case it looks like it required due to the 'box_ref' type).

    Allan
  • BelisariusBelisarius Posts: 26Questions: 0Answers: 0
    Include away. I've got one for dd/mmm/yyyy as well but I want to tweak it a bit first for the regex first. I'll add it here when done.


    re the type detection. I think that was my point from above. What sequence does it check them? If it hits the javascript default date first then it will id that and use that (although only in cases where the dd/mm and mm/dd are both valid) else for something like 31/12 it'll skip to my type and use that correctly - unless you specify the sType for the column?

    Cheers

    Andy
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Ah yes I see what you mean. The order it checks it in is the internal types first and then the plug-in ones. Thinking about it it should perhaps be that it checks the plug-ins first. I'll see if I can tweak this...

    Allan
  • cmoozcmooz Posts: 21Questions: 0Answers: 0
    where do i have to add the functions?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi cmooz,

    Have a look at the plug-ins example: http://datatables.net/examples/example_sorting_plugin.html . This should show you how you can add plug-ins.

    The other examples (which are all linked from http://datatables.net/usage ) can be quite useful as well.

    Allan
  • dh1dh1 Posts: 3Questions: 0Answers: 0
    i get a 404 error at the following two pages:
    http://www.datatables.net/examples/api/dom_sort.html
    and
    http://datatables.net/examples/example_sorting_plugin.html

    have they been moved?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yup - both been moved to organise the examples a bit more:

    http://datatables.net/examples/plug-ins/dom_sort.html
    http://datatables.net/examples/plug-ins/sorting_plugin.html

    No example has been removed yet, so they are all available from http://datatables.net/examples/ .

    Allan
This discussion has been closed.