0

I've order number inside of the array object. How can I sort data object with order?

data =[
{
name:''
list:{
order :2
},

{
name:''
list:{
order :1
}
]

1 Answer 1

1

Array.sort with a custom comparer function

const data = [
  { name: '', list: { order: 2 },
  { name: '', list: { order: 1 }
];

const ascending = data.sort((a,b) => a.list.order - b.list.order);
const descending = data.sort((a,b) => b.list.order - a.list.order);
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.