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

185
Views
How to properly sort documents in firebase collection from first to last

I want every data sent last to be sorted as the last mongo document in my firebase collection. In any case, the last sent data will be stored as the last document in the collection. I used sorting by dates and current time but it is not effective for all dates. (for example date 2.6 puts first instead of 19.6)

This is my function:

Reservations(){
  this.user = store.currentUser
  this.empty_form=false;
  if(this.current_museum!=''&&this.date!=''&&this.ticket_number!=''&&this.user!='')
  {
  //Date I used for sorting  
  const current = new Date();
  const date = current.getFullYear()+'-'+(current.getMonth()+1)+'-'+current.getDate();
  const time = current.getHours() + ":" + current.getMinutes() + ":" + current.getSeconds();
  const dateTime = date +' '+ time;

  let counter = counter + 1;

  router.replace({path:'/ticket_reserved', replace: true})    
  

  try {
    db.collection("reservations")
        .doc(dateTime)
        .set({
          current_museum: this.current_museum,
          date: this.date,
          ticket_number: this.ticket_number,
          user: this.user
        });
    
  }
  catch(error) {
    
  }
}else{
  this.empty_form=true;
}},
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I used sorting by dates and current time but it is not effective for all dates. (for example date 2.6 puts first instead of 19.6)

This is a classical problem with date sorting. One common solution is to store the data in the following format: YYYYMMDD (e.g. 20220602 and 20220619). This way the lexicographic ordering is correct.

You can very well duplicate the date field in your Firestore document: one date field with the date in the desired date format (most probably of type Timestamp) and another field, e.g. dateSorting, using the YYYYMMDD format and of type String. You use the second field for building your query.

Note that it works also for date/time format, e.g. YYYYMMDD-HHMMSS.

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!