netx netx - 2 months ago 43
AngularJS Question

orderby date angularjs ng-repeat

i'm trying to orderby 'date' together with ng-repeat but my date format is "date": "Sun, 01 Sep 2016 08:47:53 -0400". i tried using momentjs but i'm getting a wrong list.

pls, how do i orderby date

http://codepen.io/netwrkx/pen/WGbwgY?editors=101

<tr ng-repeat="expense in expenses | orderBy:'date'">
<td>{{expense.name}}</td>
<td>{{expense.amount}}</td>
<td>{{expense.date | amTimeAgo}} </td>
</tr>

R.J R.J
Answer

You can create your custom filter dateOrder which gives input to in built filter.

JS :

.filter('dateOrder',function(){

   return function(data){ 
     angular.forEach(data,function(val,key){
       val.dateTimestamp = new Date(val.date).getTime()

     });
     return data; 

   }  
});

Define your html as :

<tr ng-repeat="expense in expenses | dateOrder |orderBy :'dateTimestamp'">

Here is the working codepen

Comments