1

I am iterating through a list of objects in an ng-repeat, and I want to sort them based on an attribute called approved. approved can be 'APPROVED', 'DENIED', or 'PENDING'. I want the pending to appear first, followed by the Approved, then the Denied. Sorting alphabetically wont get me this result, and i'm unsure if there is a way to specify an order to sort by using orderBy. currently it sorts alphabetically and by date:

orderBy: ['approved', 'date']

Is there any way to use orderBy to achieve this result?

Thanks.

2
  • Perhaps you need a object that has a sort priority property on it or an order propetry on it, Pending =1, etc... You bind to the name, but you sort by the priority/order priority Commented Dec 16, 2016 at 14:34
  • 1
    You can write a custom order by function Commented Dec 16, 2016 at 14:35

1 Answer 1

2

Perhaps you need a object that has a sort priority property on it or an order property on it, Pending =1, etc... You bind to the status, but you sort by the priority/order priority

[{status:'pending', order:1},{status:'approved', order:2},{status:'denied', order:3}
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! I'm rather unexperienced in Javascript and Angular and i've just got one question: The way I would implement this is I would grab the data from the backend and assign it to a variable, lets call it objects. Right now to give it the order I would run a loop and loop through all the objects in objects and set order that way. Is there a better way to do this with angular? Thanks!
If this is coming from a database I assume you have a code table of some sort that has pending, approved, denied in it, If possible I would put the order in there, if not, then the actual API, if not there then you have to do some hack where you loop through all the objects you return and add the order property on there, I would hate that because it would be brittle if you add a new code. I would need more information to give you the best solution, just do what is best for the next person who has to figure it out..
Is this data coming from a database you have access to? or a WEB api?

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.