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

204
Visualizações
Sort array based on string transformed into date

So I have an array containing reviews (which I retrieve from firebase firestore). The field 'date' is string in firestore. How can I sort the reviews in descending order based on this date? I have tried this but they are in the retrieval order.

const getReviews=async()=>{
        let reviewsClone=[];
        const q = query(collection(db, "reviews"), where("product", "==", uniqueProductName.product));
        const querySnapshot=await getDocs(q);
        querySnapshot.forEach((doc)=>{
            reviewsClone.push(doc.id);
        })

        reviewsClone.sort(function(a,b){
            return new Date(a.date) - new Date(b.date);
          });
        setReviews(reviewsClone);

    }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Could you try this? It's likely that you are sorting an array of strings reviewsClone currently, which all have .date prop undefined, therefore .sort has no effect.

const getReviews=async()=>{
        let reviewsClone=[];
        const q = query(collection(db, "reviews"), where("product", "==", uniqueProductName.product));
        const querySnapshot=await getDocs(q);
        querySnapshot.forEach((doc)=>{
            reviewsClone.push(doc);
        })

        reviewsClone.sort(function(a,b){
            return new Date(a.date) - new Date(b.date);
          });
        setReviews(reviewsClone.map(c => c.id));

    }
about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming that all the data is valid, and contains the date key. Your sort function is incorrect. To sort in descending order, your sort function should look like this:

reviewsClone.sort(function(a,b){
        return new Date(b.date) - new Date(a.date);
      });

This documentation should be helpful.

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