Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

446
Views
How to order comments in firebase firestore by createdAt

I have a document in which there is a comments array of objects and each object has createdAt property. I want to sort all the comments that are inside the comments array using the createdAt property, so, a new comment comes to the top.

I did some research and found that we can do this in firebase's real-time database but I want to do the ordering of data using firestore.

Here is my code:

import { useEffect, useRef, useState } from "react"
// firebase import
import { doc, onSnapshot, orderBy, query } from "firebase/firestore"

import { db } from "../firebase/config"

export const useDocument = (c, id, o) => {
  const [document, setDocument] = useState(null)
  const [error, setError] = useState(null)

  // realtime document data
  useEffect(() => {
    let docRef = doc(db, c, id)

    if (o) {
      docRef = query(docRef, orderBy("createdAt", "desc")) // this is not working
    }

    const unsubscribe = onSnapshot(
      docRef,
      (snapshot) => {
        // need to make sure the doc exists & has data
        if (snapshot.data()) {
          setDocument({ ...snapshot.data(), id: snapshot.id })
          setError(null)
        } else {
          setError("No such document exists")
        }
      },
      (err) => {
        console.log(err.message)
        setError("failed to get document")
      }
    )

    // unsubscribe on unmount
    return () => unsubscribe()
  }, [c, id])

  return { document, error }
}

createdAt property: enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The query() function takes a query as parameter and not a DocumentReference. Also the orderBy() clause orders documents when fetching multiple documents from a collection and not array elements.

To order array elements, you first need to fetch that document and then manually sort the array.

const unsubscribe = onSnapshot(
  docRef,
  (snapshot) => {
    // need to make sure the doc exists & has data
    if (snapshot.data()) {
      const orderedArray = snapshot.data().comments.sort((a, b) => a.createdAt.seconds - b.createdAt.seconds);
    } else {
      setError("No such document exists")
    }
  }
)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!