I have an unsorted array of object having 2 revelant property: type, and date.
I need to sort this array:
- By date and
- By type (for every year,
depositshould go first beforewithdrawal)
Here is an example of input:
let i = [
{
date:'2017',
type:'withdrawal',
amount:-5
},
{
date:'2016',
type:'deposit',
amount:12
},
{
date:'2018',
type:'deposit',
amount:54
},
{
date:'2017',
type:'deposit',
amount:20
},
{
date:'2016',
type:'withdrawal',
amount:55
},
{
date:'2018',
type:'withdrawal',
amount:54
}
]
The goal is to output something like this:
let o = [
{
date:'2016',
type:'deposit',
amount:12
},
{
date:'2016',
type:'withdrawal',
amount:55
},
{
date:'2017',
type:'deposit',
amount:20
},
{
date:'2017',
type:'withdrawal',
amount:-5
},
{
date:'2018',
type:'deposit',
amount:54
},
{
date:'2018',
type:'withdrawal',
amount:54
}
]
So far I managed to sort the array by date using:
o = i.sort((a,b)=>(a.date - b.date))
But I can't find a way to sort it by type