0

I manually created a set of unordered entries here and I was wondering how I could properly apply the orderBy to iterate through the "order" entry.

I did use the code below but it still returns the same unordered entries I first created.

_.orderBy(rows, 'order', 'asc')

What am I doing wrong here? :(

1
  • Are you using the list returned from the orderBy call? lodash doesn't sort the list in-place. Commented Aug 22, 2017 at 5:35

1 Answer 1

2

Hey there I think you only got a small error inside your idea, this one should be working:

const _ = require('lodash')

const UserOrders = [
  [{ order: '2' }, { order: '1' }, { order: '3' }],
  [{ order: '3' }, { order: '1' }, { order: '2' }],
]

const sortedUserOrders = UserOrders.map(orders =>
    _.orderBy(orders, ['order'], ['asc'])
)

this is the output of my code:

​​​​​[ [ { order: '1' }, { order: '2' }, { order: '3' } ],​​​​​
​​​​​  [ { order: '1' }, { order: '2' }, { order: '3' } ] ]

I also recommend you to take a look into the lodash documentation

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

2 Comments

Thank you so much! This did the trick, I knew I had to use .map somewhere when I kept stumbling upon it on my searches.
@RoboKy feel free to upvote it aswell, if liked the answer!

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.