Sort Datatables in DD-MM-YYYY

1

I have a datatable with some dates and these do not take the format of DD-MM-YYYY I have been testing in several ways (especially this but I do not quite understand the operation exactly.

$(document).ready(function() {
  $('#tabla_clientes').dataTable({

    "aaSorting": [
      [0, "desc"]
    ],
    "aoColumnDefs": [{
      'bSortable': false,
      'aTargets': [5]
    }]
  });
});

I have that code and it looks like this:

He does not order the dates as dates, but as if they were text. What is failing?

    
asked by Jesús 20.03.2017 в 17:16
source

1 answer

1

The sort by default takes the values and orders them, does not know what you mean dates, and being string does it as it is.

If the format were YYYY / MM / DD, it would do well automatically.

If you do not have that, you can modify the sorting algorithm as you see here: link

It would be doing something like:

$('#tabla_clientes')
    .column( 0 )
    .data()
    .sort((a, b) => new Date(a) - new Date(b));

The - of the sort function depends on whether you want to sort it down or up.

Greetings.

    
answered by 04.04.2017 в 16:07