My array object is like below example.
list = [
{'name': 'test1', 'email': 'test1@gmail.com', 'ispin': true, 'updatedAt': 1540456416646}
{'name': 'test2', 'email': 'test2@gmail.com', 'ispin': false, 'updatedAt': 1540456416111}
{'name': 'test3', 'email': 'test3@gmail.com', 'ispin': true, 'updatedAt': 1540456412541}
{'name': 'test4', 'email': 'test4@gmail.com', 'ispin': false, 'updatedAt': 1540456414521}
]
I'm able to sort this array object like this.
return conversationList.sort((a, b) => {
const firstTimestamp = a.lastMessage && a.lastMessage.createdAt ? a.lastMessage.createdAt : 0;
const secondTimestamp = b.lastMessage && b.lastMessage.createdAt ? b.lastMessage.createdAt : 0;
//Sort in DESC order
return secondTimestamp - firstTimestamp;
});
Using the above code I'm able to sort an array of object in descending order.
Now, my requirement is first filter array based on ispin key. if ispin is set to true then I want to create new array object with key pin also in descending order and one more array with key recent in descending order.
So my final output should be like this.
{
// first array object with key pin : filter by ispin:true and order by descending order
pin: [
{'name': 'test1', 'email': 'test1@gmail.com', 'ispin': true, 'updatedAt': 1540456416646}
{'name': 'test3', 'email': 'test3@gmail.com', 'ispin': true, 'updatedAt': 1540456412541}
],
// second array object with ket recent : order by descending order and records which has ispin false
recent: [
{'name': 'test2', 'email': 'test2@gmail.com', 'ispin': false, 'updatedAt': 1540456416111}
{'name': 'test4', 'email': 'test4@gmail.com', 'ispin': false, 'updatedAt': 1540456414521}
]
}