Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda