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

314
Views
How to sort my array of objects based on firebase Timestamp

I need my messages should be ordered based on timestamp ("createdAt" is the name of filed) i can do it in earlier versions of firebase but cant fig out how to do it in firebase v9

DATA STRUCTURE ON FIRESTORE

SEE IMAGE HERE

const [messages, setMessages] = useState([])
    useEffect(() => {

        const unsub = onSnapshot(doc(db, "messages", state.id), (snapshot) => {
            if(snapshot.data()){
                console.log(123,Object.values(snapshot.data()))
                setMessages(Object.values(snapshot.data()))
            }
        });
    }, [])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

What you have in your document is not an array. They are just fields with random IDs. You can sort them using .sort() as shown below:

if (snapshot.data()) {
  const sortedMessages = Object.values(snapshot.data()).sort((a, b) => a.createdAt.toMillis() - b.createdAt.toMillis())

  setMessages(sortedMessages);
}
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!