Web / Apache Commons Collections Interview questions
Define FixedOrderComparator in Apache Commons Collections?
FixedOrderComparator<T> sorts elements according to a custom sequence you supply up front, rather than their natural ordering or a computed rule.
FixedOrderComparator<String> statusOrder = new FixedOrderComparator<>("NEW", "IN_PROGRESS", "DONE"); List<String> tickets = new ArrayList<>(List.of("DONE", "NEW", "IN_PROGRESS")); tickets.sort(statusOrder); // [NEW, IN_PROGRESS, DONE]
It's built for business-defined orderings that don't correspond to alphabetical or numeric order - like a workflow status sequence or a fixed priority list - where writing a manual Comparator with a lookup table would otherwise be needed.
Elements not included in the predefined sequence are handled according to a configurable UnknownObjectBehavior: throw an exception, sort them before all known elements, or sort them after all known elements.
More Related questions...