Search code examples
angularjssortingangularjs-orderby

AngularJS OrderBy update source array


I'm using the built-in Angular filter OrderBy to my items by date and it works as expected in the DOM but I thought it would update the array itself so I can work with the new order of the items.

Anyone have any ideas how to achieve this?

Thanks.

P.S. I'm using AngularJS 1.3.14 if that changes anything.


Solution

  • You should re-order your array from your controller before you bind it to the DOM. This is also more performant as Angular doesn't need to recompute the order on each digest cycle.

    $scope.myOrderedArray = order( $scope.myArray );
    

    And in the DOM

    <div ng-repeat="item in myOrderedArray"> ... </div>