2

I created an ArrayList from an array.

ArrayList <Person> PersonArrayList = new ArrayList<>(Arrays.asList(PersonArray));

What is the best way to go about doing this?

Trying something like:

I have created getters getLastName() and getFirstName()

1 Answer 1

3

The easiest way would be using comparing along with thenComparing.

personArrayList.sort(Comparator.comparing(Person::getLastName)
                               .thenComparing(Person::getFirstName));
Sign up to request clarification or add additional context in comments.

2 Comments

What does’Person::getLastName’ mean?
@Skemelio it's a method reference which is equivalent to the following lambda (Person person) -> person.getLastName(). Essentially, the Comparator Comparator.comparing(Person::getLastName) .thenComparing(Person::getFirstName) first extracts the persons' last names for comparison and if they're the same extracts the persons' first names and compares them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.