1

I have an Angular ng-repeat statement which I need to order by the date field (entry_date). So far it is only ordering by the month and the day using this code:

<tbody ng-repeat="transaction in transactions | orderBy:'-entry_date'">
                                <tr>
                                    <td>
                                        <input ng-model="transaction.selected" type="checkbox"/>
                                    </td>
                                    <td>
                                        <span>{{ transaction.entry_date | date:'yyyy-MM-dd HH:mm' }}</span>
                                    </td>
                                    <td>
                                        <span>{{ transaction.description }}</span>
                                    </td>
                                </tr>
                            </tbody>
                        </table>

Here is the entry date value pulled from console: "2011-06-30T08:17:35Z"

And here is the table of dates first page:

2011-06-30 09:17    
2011-06-29 12:47    
2011-06-29 08:20    

Here is the last page:

2017-02-12 01:00    
2017-02-12 01:00    
2017-02-12 01:00    
2017-02-12 01:00    
2017-02-11 01:00    

I have also tried using a callback function:

  $scope.sortDates = function(transaction) {
    var date = new Date(transaction.entry_date);
    return date;
};

But this just returns the data in the same way as my original code.

Is there a way to get the filter to use the year AND the month/date?

Thanks in advance.

dispatj
  • 11
  • 2
  • You can use a callback ( function ) in your angularJS orderBy,i suggest using moment JS to compare and order your dates. – Nicolas Feb 13 '17 at 13:30
  • I tried this and get the same results: $scope.sortDates = function(transaction) { var date = new Date(transaction.entry_date); return date; }; – dispatj Feb 13 '17 at 15:12

1 Answers1

0

Actually, i think your need to return a integer, if the integer is positive, the first element is bigger than the second one.
if it's negative it is smaller than the second one and if the integer is equals to zero they are both the same.
You could use moment to get the unix timestamp of both date and substract them.
Something like

function(a, b){
    return moment(a).unix() - moment(b).unix();
}

I have not tested the code.

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • Thanks for the help, I had tried something like that without moment which didn't work. Just tried that and it is unfortunately still not sorting by year. All other parts of the date are sorted, day, month, time. Just not year. – dispatj Feb 13 '17 at 16:08
  • Are you trying to sort them by each part of the date ? in that case, you would need to split your date before you try to sort them. – Nicolas Feb 13 '17 at 16:24
  • Trying to sort by year then month then date. I have 6 pages of data and on the last page is the data I would expect on the first page. On the first page is the data I would expect on the last. – dispatj Feb 13 '17 at 16:29
  • Then you need to sort it in descendent order. check this link http://stackoverflow.com/questions/16261348/descending-order-by-date-filter-in-angularjs – Nicolas Feb 13 '17 at 16:34