0

I have a requirement where in I need to sort records in the descending order of DateTime type field in angular JS, used in vlocity template. If I am trying to sort just by Date type field, then the below code is working fine, however on changing the type from Date to DateTime, I am getting error : "Cannot read property 'toLowerCase' of undefined"

Code Snippet :

$scope.initializePagination = function(ctrl, ths) { var pagedItems = []; var reversedList = ctrl.vlcSI.recSet;// I getting the list of records here

    reversedList = reversedList.sort(function (a, b) {
        let months = { jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12 };
        var aa = a.adjustmentDateTime.trim().split(' ');
        var bb = b.adjustmentDateTime.trim().split(' ');
        return bb[2] - aa[2] || months[bb[1].toLowerCase()] - months[aa[1].toLowerCase()] || bb[0] - aa[0] ;
    }); 

    for (var i = 0; i < reversedList.length; i++) {
       if (i % $scope.itemsPerPage === 0) {
            pagedItems[Math.floor(i / $scope.itemsPerPage)] = [reversedList[i]];
        } else {
            pagedItems[Math.floor(i / $scope.itemsPerPage)].push(reversedList[i]);
        }
    }

    $scope.pagedItems = pagedItems;

}

Error Screenshot : enter image description here

1 Answer 1

0

You're trying to do your own parsing. There's no need. Just subtract the dates directly:

reversedList = reversedList.sort(function (a, b) {
    return new Date(b || 0) - new Date(a || 0);
});
1
  • Thanks a lot, it worked with a minor update as : reversedList = reversedList.sort(function (a, b) { return new Date(b.DateTimeFieldName || 0) - new Date(a.DateTimeFieldName || 0); }); Oct 16, 2020 at 4:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.