2

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?

1
  • 1
    You could run the same orderBy filter in the controller - $filter('orderBy')(array, expression, reverse). Not sure if that will be practical for you though depending on how many ways you let people sort. Commented Dec 3, 2014 at 18:34

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.