Auto-Sorting

Auto-Sorting

techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
edited March 2011 in General
Hi,

I was wondering if there was an auto-sorting for a date in the following format ... DD/MM/YYYY. I understand there is one for DD/MM/YY but I would like to show the full year.

Can anyone help me out with this?

Many thanks.

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Anything which is picked up by the Javascript native Date.parse() method will be used as a date by DataTables. If your date format isn't automatically picked up then you'll need to use a sorting plug-in. This one for example looks like what you are after: http://datatables.net/plug-ins/sorting#date_euro_short

    Allan
  • techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
    Sorry - I don't really understand? You mean any date is picked up automatically by the Date.Parse() function?

    All I am using is a normal date like dd/mm/yyyy.

    I am also noticing that with commas the currency is not displaying in descending order correctly. i.e. 1,000, should appear before 800, but for some reason 1,000 shows last!?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    No I mean if a string (any string, doesn't matter what) is found to be a date by Date.parse() then DataTables will treat it as a date. Your date format it would appear is not found to be a date - and kindly Andy McMaster has already provided a sorting function what this kind of date format.

    For the comma issue, have a look at this plug-in: http://datatables.net/plug-ins/sorting#formatted_numbers . 1,000 isn't strictly an integer, it's a string with alphanumerics in it, so it isn't automatically picked up as a number. The formatted numbers plug-in allows it to be sorted as would be expected.

    Allan
  • techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
    Hi Allan,

    Thanks for you're help, unfortunately my numbers include a currency symbol and commas! What a disaster! Seems there is no automatic sorting for me.

    I've tried a function that someone was using for the date (dd/mm/yyyy) and I was hoping it would "auto" sort it for me, is this the case? Or do I still need to include the sType? I say this because the tables I am building are built off user generated content, so I woulden't be able to know exactly what column number the data was in.

    Thanks for you're help.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    The formatting numbers plugin should still work - did you try it? It will strip out any non-numeric data and force the value passed in to be a number. If it doesn't work, then it should be quite trivial to modify.

    If you want 'auto sorting' detection, you need to use a type detection plugin - this one for example: http://datatables.net/plug-ins/type-detection#uk_date . The alternative to a automatic type detection is to specify sType.

    Allan
  • techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
    Hi Allan,

    I have installed the number's HTML one - I am not sure if this covers commas? But anyway's for some reason it doesn't seem to be working, have you any ideas?

    [code]



    jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };

    jQuery.fn.dataTableExt.aTypes.push( function ( sData )
    {
    sData = typeof sData.replace == 'function' ?
    sData.replace( /<.*?>/g, "" ) : sData;

    var sValidFirstChars = "0123456789-";
    var sValidChars = "0123456789.";
    var Char;
    var bDecimal = false;

    /* Check for a valid first char (no period and allow negatives) */
    Char = sData.charAt(0);
    if (sValidFirstChars.indexOf(Char) == -1)
    {
    return null;
    }

    /* Check all the other characters are valid */
    for ( var i=1 ; i
  • techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
    The strange this is - as soon as I add the stype on the datatable it all works as expected.

    It would be a massive pain however if I had to go through each of my datatables and add a sorting type!!
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    If it all works when you specify the sType, than that's good - that means the sorting is at least working. So the next step is to see what might be happening to the custom type detection. Try:

    [code]
    jQuery.fn.dataTableExt.aTypes.unshift( function ( sData )
    [/code]
    'unshift' rather than 'push' and see if that helps. I have a sneaking suspicion that your code might be being treated as type 'html' by DataTables.

    Allan
  • techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
    Hi Allan,

    Sorry but that doesn't work :(

    Just doesn't seem to sort anything. P.S. I took the sType bit out to check if it was working.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Can you link us to your page please so I can see where it is going wrong?

    Allan
  • techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
    Hi Allan,

    It's a closed system (internal) - do you have an email I can send to you for access?

    Many thanks.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    You can get in touch using the contact form: http://datatables.net/contact :-)

    Allan
  • techguy1988techguy1988 Posts: 27Questions: 0Answers: 0
    Email sent :)
This discussion has been closed.