0

I've problem sorting my nested array, says I've json like this

var orders = [{
    'orderId': 1,
        'sales': [{
        'salesNumbers': 3
    }]
}, {
    'orderId': 2,
        'sales': [{
        'salesNumbers': 4

    }]
}];

and I wish I can sort orderId base on salesNumbers. You may say it's impossible or I made a mistake by putting sales as array but it contain only 1 object which is salesNumbers. That's not a mistake, I just do not want to simplify my problem.

so it's possible to, without changing the data structure, sort orderId base on salesNumbers?? My app demo http://jsfiddle.net/sq2C3/

3
  • "I just do not want to simplify my problem" -- why would you NOT want to simplify it? Commented Apr 19, 2014 at 4:40
  • Yes, it's possible. it's just like sorting any other Array of Objects. You just need to target the nested object/field to sort on. Commented Apr 19, 2014 at 4:43
  • @RUJordan because it's so complicated and tons of code to post here Commented Apr 19, 2014 at 5:54

1 Answer 1

1

Since you say the sales array only has one item in it, you can order by salesNumbers like this:

orderBy:'sales[0].salesNumbers'

Here is an update of your fiddle: http://jsfiddle.net/wittwerj/sq2C3/2/

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

5 Comments

you saved my life, I knew nothing is impossible!
just curious hw angular know which sales I refer to? I mean I can have sales in somewhere of my json too
orderBy can only see properties on the order object created by ng-repeat.
thanks. I found my problem still haven't solve, what if I've multiple item in my sales array? I can't do loop in <li>
you mean you want to sort the orders by their max salesNumbers, not the first one? for that i believe you will need a custom filter, or presort your data in the controller. see stackoverflow.com/questions/14493116/…

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.