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

205
Visualizações
Java Collection sort

I am trying to sort a list of Dates and it's not working.

Here is the declaration and get function in AttEnt

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "end_time")
private Date endTime;

public Date getEndTime() {
    return endTime;
}

Here is the sorting code that isn't doing anything. GetAttempts() gets the list of all the attempts for called. They aren't in order, and I just want to be able to get whatever attempt has the latest endTime.

            List<AttEnt> attempts = called.getAttempts();
            Collections.sort(attempts, new Comparator<AttEnt>() {
            @Override
            public int compare(AttEnt a1, AttEnt a2) {
                if (a1.getEndTime() == null || a2.getEndTime() == null)
                    return 0;
                return a1.getEndTime().compareTo(a2.getEndTime());
                }
            });

I believe that the code above should sort attempts, and then after it attempts should be sorted, so the latest end time would be attempts.get(attempts.size()-1).getEndTime()

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Comparator<AttEnt> comparator = Comparator.comparing(AttEnt::getEndTime).reversed();
attempts.sort(comparator);

Java static methods in interfaces are your friend

Click HERE for more awesomeness

over 4 years ago · Santiago Trujillo 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