I have array like this:
JavaScript
x
27
27
1
const array = [{
2
id: 1,
3
date: 591020824000,
4
isImportant: false,
5
},
6
{
7
id: 2,
8
date: 591080224000,
9
isImportant: false,
10
},
11
{
12
id: 3,
13
date: 591080424000,
14
isImportant: true,
15
},
16
{
17
id: 4,
18
date: 591080225525,
19
isImportant: false,
20
},
21
{
22
id: 5,
23
date: 591020225525,
24
isImportant: true,
25
},
26
];
27
And I’m trying to sort this array so elements that are isImportant: true with the most recent date are displayed first and then elements that have isImportant:false with the most recent date
Advertisement
Answer
JavaScript
1
8
1
array.sort(function(a,b){
2
if(a.isImportant && !b.isImportant)
3
return -1;
4
if(!a.isImportant && b.isImportant)
5
return 1;
6
return b.date-a.date;
7
});
8
I hope this will resolve your issue