How do I sort the final result based on the timestamps after concatenating 2 arrays?

const timeline = async (req, res) => {
  const currentUser = await users.findById(;
  const currentUserPost = await posts.find({ userId: currentUser._id });

  const friendsPost = await Promise.all( => {
      return posts.find({ userId: f });

  const finalpost = currentUserPost.concat(...friendsPost);
  //How do I sort the finalpost array with its time timestamps?

Hi @Anthony_Ezeh welcome to the community!

I assume you’re using Mongoose? Could you post the schema or some example documents? It’s hard to answer your question without all the information at hand.

Best regards