Courseiva
Working with Arrays and CollectionsmediumMultiple ChoiceObjective-mapped

1Z0-829 Working with Arrays and Collections Practice Question

A developer is implementing a custom sort for a list of Employee objects. The Employee class has fields: String name, int age. The list must be sorted first by name (ascending, case-insensitive), then by age (descending). Which Comparator implementation correctly achieves this?

⚠ Common exam trap

The trap here is that candidates often forget to reverse only the secondary comparator (age) and instead apply `reversed()` to the entire chain, which inverts all sort orders, or they mistakenly use `thenComparingInt` without reversal, leading to ascending age order.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER).thenComparing(Comparator.comparingInt(Employee::getAge).reversed())

It first sorts by name using a case-insensitive comparator, then chains a reversed comparator for age, ensuring descending order for age while preserving the primary sort by name. The `thenComparing` method correctly applies the reversed age comparator as a secondary sort, which is essential for multi-field sorting where one field requires descending order.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER).thenComparingInt(Employee::getAge)

    Why it's wrong here

    Sorts by name case-insensitive ascending, then age ascending - age should be descending.

  • Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER).thenComparing(Comparator.comparingInt(Employee::getAge).reversed())

    Why this is correct

    Correctly sorts by name case-insensitive ascending, then age descending.

  • Comparator.comparingInt(Employee::getAge).reversed().thenComparing(Employee::getName, String.CASE_INSENSITIVE_ORDER)

    Why it's wrong here

    Sorts by age descending first, then name - name should be primary key.

  • Comparator.comparing(Employee::getName).thenComparingInt(Employee::getAge).reversed()

    Why it's wrong here

    Reverses entire comparator, so name descending, age descending - name should be ascending.

About these practice questions

One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.