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

209
Views
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 answers
Answer question

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 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!