Sort the date on the grid chronologically?

0
 $.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var min  = $('#min-date').val() ? $('#min-date').val().split('/') : null;
        var max  = $('#max-date').val() ? $('#max-date').val().split('/') : null;

        if(max && min){
          max = moment(max[2] + '-' + max[1] + '-' + max[0])

          min = moment(min[2] + '-' + min[1] + '-' + min[0])
        }

        var createdAt = data[4].substring(0,10); // Our date column in the table
        createdAt = createdAt.split('/')
        dataCriacao = createdAt[2] + '-' + createdAt[1] + '-' + createdAt[0] 

        if( (!max || !min) || moment(dataCriacao).isSameOrBefore(max) && moment(dataCriacao).isSameOrAfter(min)) {
          return true
        }

      return false

    }
);
    
asked by anonymous 02.05.2018 / 14:36

1 answer

0

I've done an example of how your rule should be with another lib. [dayjs] - Follow the link if you'd like to check it out.

const dayjs = require('dayjs');

let x = [
    { data: dayjs(new Date('01-01-2018')).format('DD-MM-YYYY') } , 
    { data: dayjs(new Date('07-06-1996')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-03-1981')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-02-2005')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-05-2003')).format('DD-MM-YYYY') }, 
    { data: dayjs(new Date('07-02-2001')).format('DD-MM-YYYY') }
];

var global;

function sortData(){
    global = x.sort( (a,b) => {
        if (dayjs(a.data).isBefore(dayjs(b.data)))
            return -1 
        else 
            return 1
    }) 


    console.log(global);
}

sortData();

However, what matters to you is the sort that will sort to you according to the last callback function.

    
02.05.2018 / 16:06