Home > Mobile >  Angular 6 sort JSON array based on inside array key
Angular 6 sort JSON array based on inside array key

Time:03-31

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
}
]

CodePudding user response:

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);
  • Related