Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SORTED array after using orderBy Angular

Tags:

angularjs

I have an array I have sorted with the Angular orderBy command in my html. I need to retrieve the items in the list in the order in which they are sorted. Is there a way to do this?

using $scope.people[] ng-repeat makes a table as follows:

Original
id    name
2     bob
4     joe
1     dave

After sorting I get
id    name
1     dave
2     bob
4     joe

Now I want to be able to access $scope.people[] but in the sorted order. Is this possible?

like image 707
user3413723 Avatar asked Feb 14 '26 14:02

user3413723


1 Answers

Angular 1.3.x added "alias" expression which will store the intermediate results of the repeater after the filters have been applied.

    <div ng-repeat='p in people | orderBy : "name" as result'>
        {{p.name}}
    </div>

    <p> <b>Original</b></p>
    <div>{{people || json}}</div>

    <p><b>Sorted</b></p>
    <div>{{result || json }}</div>

Plunkr

like image 191
Andy Ecca Avatar answered Feb 17 '26 05:02

Andy Ecca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!