020-001-014
Collections.sort() with Comparator
Medium
Problem Description
Collections.sort() with Comparator
In this problem: You will use Collections.sort() with a Comparator to sort ArrayList elements in custom order.
Learning Objective: Understand custom sorting with Collections.sort() and Comparator
Overview
Collections.sort() sorts a list in natural order, but passing a Comparator as the second argument enables custom ordering.
Specifications
- Add
"banana","apple","cherry","date"to anArrayList<String> - Sort alphabetically and output
- Sort by string length ascending and output
- Sort by string length descending and output
Output Format
Alphabetical: [apple, banana, cherry, date]
By length: [date, apple, banana, cherry]
By length desc: [banana, cherry, apple, date]
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up